Everything was good until the establishment of the relation. I am
unable to go any further than “Linking Models Together” : I can create
the “vendors” mentionned in the exemple, and I can get an “event” to
look to. But when I try to create an “expense”, I get a message
saying :
event.expenses.create(:vendor => vendor1, :amount => 75.00)
NoMethodError: undefined method expenses' for #<Event:0x125e008> from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/ attribute_methods.rb:205:inmethod_missing’
from (irb):2
Could anybody help me ? I have checked the classes files, everything
seems according to the article. I did checks on the database, the
table “expenses” has been created but is empty. The “id” fiels are
present in each of the tables… I don’t see where to look to now.
event.expenses.create(:vendor => vendor1, :amount => 75.00)
NoMethodError: undefined method expenses' for #<Event:0x125e008> from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/ attribute_methods.rb:205:inmethod_missing’
from (irb):2
Could anybody help me ? I have checked the classes files, everything
seems according to the article. I did checks on the database, the
table “expenses” has been created but is empty. The “id” fiels are
present in each of the tables… I don’t see where to look to now.
Do you have the following in your Event class:
has_many :expenses
?
This is the line that give the Event class the method “expenses” which
links to the Expense class. And in the Expense class:
belongs_to :event
to get the map the other way around giving the Expense class the method
“event”.
To easily remember what Mark told you, just remember that the foreign
key is always in the model that has the belongs_to relationship… When
I first started playing with AR, that little fact made everything much
more easier to comprehend…
Yes. Was the error produced in a console that you started after making
the extra changes to the Event and Expense models? In a console, the
models are loaded when first used only and if you change them then you
need to reload them. You can do this with:
reload!
or just by restarting the console (use “quit” to exit, then run the
console again).
In fact, I have been fooled by XCode that does not save the files when
it tells you that it does… I had to close completely XCode for some
modifications to be taken into account by rails. I have noticed this
when I had to restart my computer (battery low), my files where in an
old version. When I retyped the missing code I made typos, and I saw
them appear in the logs. The only solution to correct them has been to
close completely XCode and to execute a reload! in the console… A
bit strange.
Thanks to all and sorry for your time,
I had this exact same problem. Found this page searching for a
solution to my “NoMethodError” blues. Had to close XCode and re-open
it for the changes to be recognized. That is a VERY annoying
problem.
Also had to quit the ruby console and restart it. Using the “reload!”
command mentioned above did not work.
Sorry if this is post is in the wrong place. I am a ROR newbie also
working thru the “Developing Rails Applications on Max OSX Leopard”
article. I am having a separate issue though.
I’m wondering if my version (Rails 2.1.1) differs from that in the
tutorial(2.0.2) in the way it returns a sum: