Thierry
2008-Mar-08 19:42 UTC
Newbie worries with "Developing Rails Applications on Max OSX Leopard"
Hello There, As a new comer to RoR, I started to follow the article http://developer.apple.com/tools/developonrailsleopard.html. In short you create to entities and one one-to-many relation linking them. 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:in `method_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. Thanks in advance, Thierry --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mark Bush
2008-Mar-08 20:03 UTC
Re: Newbie worries with "Developing Rails Applications on Ma
Thierry wrote:>>> 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:in `method_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". -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ilan Berci
2008-Mar-08 20:15 UTC
Re: Newbie worries with "Developing Rails Applications on Ma
> > ThierryTo 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.. hth ilan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thierry
2008-Mar-08 20:25 UTC
Re: Newbie worries with "Developing Rails Applications on Ma
Mark, Thanks for your answer. Precisely, it seems to me that I have everything right. I put the code here : ===== > In the "event.rb" file : class Event < ActiveRecord::Base validates_presence_of :name validates_numericality_of :budget, :greater_than => 0.0 has_many :expenses has_many :vendors, :through => :expenses def total_expenses expenses.sum(:amount) || BigDecimal("0.0") end def budget_exceeded? total_expenses > budget end end ===> In the expense.rb file : class Expense < ActiveRecord::Base belongs_to :event belongs_to :vendor end ===> In the vendor.rb file : class Vendor < ActiveRecord::Base has_many :expenses has_many :events, :through => :expenses end Thanks again On 8 mar, 16:03, Mark Bush <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Thierry wrote: > >>> 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:in `method_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". > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mark Bush
2008-Mar-08 20:44 UTC
Re: Newbie worries with "Developing Rails Applications on Ma
Thierry wrote:> Thanks for your answer. Precisely, it seems to me that I have > everything right. I put the code here :That looks fine. Can you post the results of running (in the application console): Event.column_names and: Expense.column_names Just to confirm that these two models are both set up correctly too... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thierry
2008-Mar-08 21:09 UTC
Re: Newbie worries with "Developing Rails Applications on Ma
That''s it :>> Expense.column_names=> ["id", "event_id", "vendor_id", "amount", "created_at", "updated_at"]>> Event.column_names=> ["id", "name", "budget", "created_at", "updated_at"]>> Expense.column_names=> ["id", "event_id", "vendor_id", "amount", "created_at", "updated_at"]>> Vendor.column_names=> ["id", "name", "email", "created_at", "updated_at"]>>It looks fine too, doesn''t it ? On 8 mar, 16:44, Mark Bush <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Thierry wrote: > > Thanks for your answer. Precisely, it seems to me that I have > > everything right. I put the code here : > > That looks fine. Can you post the results of running (in the > application console): > > Event.column_names > > and: > > Expense.column_names > > Just to confirm that these two models are both set up correctly too... > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mark Bush
2008-Mar-08 21:21 UTC
Re: Newbie worries with "Developing Rails Applications on Ma
Thierry wrote:> That''s it : > >>> Expense.column_names > => ["id", "event_id", "vendor_id", "amount", "created_at", > "updated_at"] >>> Event.column_names > => ["id", "name", "budget", "created_at", "updated_at"]> It looks fine too, doesn''t it ?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). -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thierry
2008-Mar-08 21:31 UTC
Re: Newbie worries with "Developing Rails Applications on Ma
I did it as follows : $ script/console Loading development environment (Rails 2.0.2)>> event = Event.find_by_name(''Bienvenue'')=> #<Event id: 4, name: "Bienvenue", budget: #<BigDecimal: 125d57c,''0.456E3'',4(8)>, created_at: "2008-03-05 17:33:11", updated_at: "2008-03-05 17:33:11">>> event.expenses.create(:vendor => vendor1, :amount => 75.00)NoMethodError: undefined method `expenses'' for #<Event:0x125dd88> from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/ attribute_methods.rb:205:in `method_missing'' from (irb):2>>I am really lost...> > reload! > > or just by restarting the console (use "quit" to exit, then run the > console again). > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thierry
2008-Mar-08 23:00 UTC
Re: Newbie worries with "Developing Rails Applications on Ma
SOLVED 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, Thierry On 8 mar, 17:31, Thierry <Thierry.Jeanne...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I did it as follows : > > $ script/console > Loading development environment (Rails 2.0.2)>> event = Event.find_by_name(''Bienvenue'') > > => #<Event id: 4, name: "Bienvenue", budget: #<BigDecimal: > 125d57c,''0.456E3'',4(8)>, created_at: "2008-03-05 17:33:11", > updated_at: "2008-03-05 17:33:11">>> event.expenses.create(:vendor => vendor1, :amount => 75.00) > > NoMethodError: undefined method `expenses'' for #<Event:0x125dd88> > from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/ > attribute_methods.rb:205:in `method_missing'' > from (irb):2 > > > > I am really lost... > > > > > reload! > > > or just by restarting the console (use "quit" to exit, then run the > > console again). > > > -- > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---