3

I have not done a whole lot with this Jquery-tokeninput or Rails virtual attributes but have been slamming my head against the wall with this one. Any help or guidance is appreciated.

I have a virtual attribute reader in my announcement model which I need to validate presence of:

attr_reader :classroom_tokens
validates :classroom_tokens, :presence => true``

followed by the getter and setter:

def classroom_tokens=(ids)
    self.classroom_tokens = ids.split(",")
end

def classroom_tokens
    #Tried several things here
end 

I really just need to make sure params[:announcement][:classroom_tokens] is not empty. The validate call above seems to be looking at something else since it things it is always empty no matter what. What am I missing? Any help is greatly appreciated.

Rails 3.1 Ruby 1.9.2

UPDATE: If I do

#Announcement MODEL
attr_reader :classroom_tokens
#validates :classroom_tokens, :presence => true
def classroom_tokens=(ids)
  @classroom_tokens = ids.split(",")
end

#Announcement_controller create action
puts "Token=>#{@announcement.classroom_tokens}|"
puts "Params=>#{params[:announcement][:classroom_tokens]}|" 

I get:

Token=>|
Params=>7,13,12|
Yuri
  • 1,261
  • 1
  • 11
  • 23
  • Validations occur on models. Models shouldn't know anything about the params that were used to build them. classroom_tokens is either there or it's not. You are complicating things by trying to add your own setter/getter methods on ActiveRecord objects, since ActiveRecord is trying to do this for you. If you need to set classroom tokens from a comma_delineated array, make a different method for that. If you need a special getter, name it something other than the attribute name. – Marc Talbot Mar 13 '12 at 03:46
  • Thanks for the reply. With classroom_tokens being a virtual attribute I added the setter there just to help with the comma_delineated assignment and have been under the impression I am messing up with the getter. The setter is working just fine. What I did also was get read of the getter and setter completely but am still with the same issue where it thinks it is always empty. – Yuri Mar 13 '12 at 04:05

1 Answers1

8

Instead of setting self.classroom_tokens, just set the instance variable @classroom_tokens, and then remove the classroom_tokens method, since you're implicitly defining it by using attr_reader. The code should look like:

attr_reader :classroom_tokens
validates :classroom_tokens, :presence => true``

def classroom_tokens=(ids)
    @classroom_tokens = ids.split(",")
end
Ben Taitelbaum
  • 7,343
  • 3
  • 25
  • 45
  • ahh...I did not know I attr_reader would implicitly define it but in retrospect it makes sense. On the other hand it gives me the same result "blank" error. A quick look at the parameters tells me it is there yet it will not save it. I do not have any other model code handling this attribute. "announcement"=>{"classroom_tokens"=>"13", "subject"=>"no subject", "message"=>"adsfadsfasdf", "location"=>"adsfasdf", "date"=>"27-03-2012", "allow_email"=>"0", "allow_call"=>"0", "allow_sms"=>"0"}} – Yuri Mar 13 '12 at 04:15
  • 1
    `update_attributes` works for me when I call `user.update_attributes(:classroom_tokens => "foo,bar")`, so I suspect the issue may be in how you're calling that? Are you calling `user.update_attributes(params["announcement"])`? Also make sure you have `classroom_tokens` in `attr_accessible` – Ben Taitelbaum Mar 13 '12 at 04:44
  • Hey Ben, thanks for the reply. As the update above demonstrate somehow the attribute is not being set as the params is present but not the virtual attribute. Does it make sense the assumptions above on virtual attributes? – Yuri Mar 13 '12 at 04:45
  • 1
    DONE! You nailed it! I forgot to add it to attr_accesible. I am a little embarrassed. Thank you so much. – Yuri Mar 13 '12 at 04:48
  • @Yuri can you show how/where you're calling update_attributes? – Ben Taitelbaum Mar 13 '12 at 04:48
  • It's easy to miss, and there should probably be a flag to make it throw an exception during development rather than just print a warning. I'm guessing the attr_protected/attr_accessible logic will change to be more usable/secure in the next version of rails, hopefully. – Ben Taitelbaum Mar 13 '12 at 04:50
  • I am betting on it. =). For now is there a config setting I can set to throw an exception in the future for this scenario? – Yuri Mar 13 '12 at 04:57
  • It did indeed: http://stackoverflow.com/questions/9678778/rails-3-config-setting-for-attr-accesible-protected – Yuri Mar 13 '12 at 05:23