Can you please help me with the following problem? This I know is basic, but I seem to be missing something very basic. When I introduced ':editors' things seem to go wrong though I am pointing to the correct class name. Thanks for your help.
I have defined my model 'page' as below -
class Page < ActiveRecord::Base
belongs_to :subject
has_many :sections
has_and_belongs_to_many :editors, :class_name => "AdminUser"
end
My AdminUser.rb is defined as below
class AdminUser < ActiveRecord::Base
has_and_belongs_to_many :pages
has_many :section_edits
scope :named, lambda {|first,last| where(:first_name => first, :last_name => last)}
end
BUT when I go to rails console and type
Loading development environment (Rails 3.2.1)
>> page.editors
NameError: undefined local variable or method `page' for main:Object
from (irb):1
Hey I'm doing the same training videos that you are doing and had the same problem. What I did was re-watch the 'many to many associations: simple' video and made sure I had the code copied exactly like the instructor and made sure to SAVE! the .rb files when I was done changing the code. I found that I had copied the code exactly like the instructor but didn't save one of the .rb files before starting my Rails Console and so I received the error. Also, after inserting the code that the instructor tells you to do (and saving the.rb file), make sure you exit the Rails Console and restart it so that all the .rb files are updated. I hope this helps you out!
One other thing, I don't have the line
has_many :sections_edits
in my admin_user.rb. This could be because you're farther along than me and I'm not sure if this would make a difference. Good luck and let me know if you need further help since we are working through the same training tutorials.
Where are you initializing 'page'? The collection of owned editors belongs to a specific page, not Page in general. Fish out a page: page = Page.find([some criterion]). Then page.editors will return the editors for that page.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.