SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calling model methods even when the param is a not set

    Hello,
    I'm setting a checkbox values based on the return of a model field method. The way the method is set up - it only gets called when a checkbox value is set.

    Here's the model code:

    Code:
      def within=(new_within)
         @within = new_within == "1" || new_within == true || new_within == 'true' || new_within == 'on'
      end
    I would like that method to be called even when the checkbox is not set.

    Thanks for any help anyone can provide.

    Clem C

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It depends when this needs to be triggered (not clear from you post I think). Have you looked at the ActiveRecord callback methods? One of those might be what you are looking for.

  3. #3
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why don't you just set the @within to false by default? (in intialize for example)

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems like a callback method would be a bit too much overkill for this situation.

    And, in the model class, within is already being set by the has_location? method.

    Would it be crazy to still set the instance variable @within = false in the initialize method?


    def initialize(session = {})
    self.from_date = default_from_date
    self.to_date = default_to_date
    self.distance = 25
    self.city = session[:city]
    self.state = session[:state]
    self.postal_code = session[ostal_code]
    self.country = session[:country]
    self.within = has_location?
    if self.country.nil? || self.country.empty? || self.country == 'US'
    self.units = session[:units] #'miles'
    else
    self.units = session[:units] #' 'km'
    end
    RAILS_DEFAULT_LOGGER.debug("****ShowSearchQuery INIT || self.within-->>#{self.within}")
    end

  5. #5
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,332
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    This looks like a great place for the ternary operation:
    @within = (new_within == 'on') ? true : false
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    unfortunately, that would work perfectly but, the within=(new_within) method is not called when the checkbox is blank.

  7. #7
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    shameless boot!

  8. #8
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't understand what your problem is. If you want to have a checkbox to edit the within field you should just delete the within method and make a checkbox with a rails helper and it will work. Oh, and delete the code in your initialize(): it does not belong there. You should just save the entire object in the session, not just the fields.

  9. #9
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's basically what I'm trying to do:

    I'm first setting show_search_query like this:

    Code:
    show_search_query = session[:last_search_show] || ShowSearchQuery.new(session)
    Then every call, we set the show_search_query attributes to the latest params
    Code:
          show_search_query.attributes = params[:searchfor_show]
    then I finally set the session to the latest show_search_query object:

    Code:
      session[:last_search_show] = show_search_query
    seems like the within method is just worthless.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •