I have a simple class method in my Score model: def self.find_score(user_id, for_user_id, competency_id) find(:first, :conditions => "user_id = ''#{user_id}'' and for_user_id = ''#{for_user_id}'' and competency_id = ''#{competency_id}''") end It gets called from within a render_collection_of_partials _competency.rhtml, which in turn is nested in another render_collection_of_partials, _user.rhtml. It gets called thus (@user is set in the controller, user.id by the user r_c_o_p, and competency.id set by the competency r_c_o_p) <% my_score = Score.find_score(@user.id, user.id, competency.id) %> <%= my_score[:score] %> But I get an undefined method error [] for NilClass Now, if I debug score, I get this: --- !ruby/object:Score attributes: score: "3" competency_id: "12" id: "1283" for_user_id: "43" user_id: "61" I want to output score, ie 3 but get the above error. Any clues as to what I am missing would be appreciated. I don''t get why I can''t reference the value of score in that hash as my_score[:score]
Hi Richard, try <%= my_score.score %> also you should never use straight sql in your query, replace it by something like this that will prevent your code from sql injection: find(:first, :conditions => ["user_id=? AND for_user_id=? AND competency_id=?", user_id, for_user_id, competency_id]) bests, Richard> I have a simple class method in my Score model: > > def self.find_score(user_id, for_user_id, competency_id) > find(:first, :conditions => "user_id = ''#{user_id}'' and for_user_id > = ''#{for_user_id}'' and competency_id = ''#{competency_id}''") > end > > It gets called from within a render_collection_of_partials > _competency.rhtml, which in turn is nested in another > render_collection_of_partials, _user.rhtml. > > It gets called thus (@user is set in the controller, user.id by the > user r_c_o_p, and competency.id set by the competency r_c_o_p) > > <% my_score = Score.find_score(@user.id, user.id, competency.id) %> > > <%= my_score[:score] %> > > But I get an undefined method error [] for NilClass > > Now, if I debug score, I get this: > > --- !ruby/object:Score > attributes: > score: "3" > competency_id: "12" > id: "1283" > for_user_id: "43" > user_id: "61" > > I want to output score, ie 3 but get the above error. Any clues as to > what I am missing would be appreciated. I don''t get why I can''t > reference the value of score in that hash as my_score[:score] > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
* Richard Sandilands <infoarts-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> [0616 00:16]:> I have a simple class method in my Score model: > > def self.find_score(user_id, for_user_id, competency_id) > find(:first, :conditions => "user_id = ''#{user_id}'' and for_user_id > = ''#{for_user_id}'' and competency_id = ''#{competency_id}''") > endThere''s probably a nicer way than that, but I barely know enough SQL to order a beer, so I''ll leave that,> It gets called from within a render_collection_of_partials > _competency.rhtml, which in turn is nested in another > render_collection_of_partials, _user.rhtml. > > It gets called thus (@user is set in the controller, user.id by the > user r_c_o_p, and competency.id set by the competency r_c_o_p) > > <% my_score = Score.find_score(@user.id, user.id, competency.id) %> > > <%= my_score[:score] %>That should work, because ActiveRecord::Base ''quacks like a Hash'', but it''s probably simpler to just use <%= my_score.score %> (which is what AR converts the above into anyway, and is easier to type :) ). It looks like my_score is nil, hence the error below, so query isn''t finding anything. In my (limited) Rails experience, now is the time to check your logs to see what sql is being generated by the above, and verify that what you think is in the DB actually *is* there.> But I get an undefined method error [] for NilClass > > Now, if I debug score, I get this: > > --- !ruby/object:Score > attributes: > score: "3" > competency_id: "12" > id: "1283" > for_user_id: "43" > user_id: "61"-- ''As adverts on the television tell us not to use light switches if we smell gas, I find it useful to always have a candle ready for use in such emergencies.'' -- Mrs D Bibby, Rugby. Rasputin :: Jack of All Trades - Master of Nuns