skip87
skip87

Reputation: 529

got an error with rails console

Hi everybody got some trouble with the rails console . here is the error.

>> Page.editors << me
NoMethodError: undefined method `editors' for #<Class:0x1038560e8>
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/dynamic_matchers.rb:50:in `method_missing'
    from (irb):5

here is the relation between the models.

class Page < ActiveRecord::Base
  attr_accessible :name, :permalink, :position
  has_and_belongs_to_many :editors, :class_name => "AdminUser"
  #has_and_belongs_to_many :AdminUser
  belongs_to :subject
  has_many :sections
end

the second one

class AdminUser < ActiveRecord::Base
   attr_accessible :first_name, :last_name, :email, :username
  has_and_belongs_to_many :pages
  scope :named, lambda{|first,last| where(:first_name => first,:last_name => last)}
end

Upvotes: 0

Views: 100

Answers (2)

deefour
deefour

Reputation: 35370

editors is an instance method on Page, not a class method of Page. You need to instantiate a new instance of Page to call editors on it.

page = Page.find(...)
page.editors << me

What you're trying to do is append me by using << on the Page class which doesn't make sense since you're not specifying which Page to append me to.

Some reading:

Upvotes: 1

Aditya Kapoor
Aditya Kapoor

Reputation: 1570

This is wrong...it should be

p = Page.first

p.editors << me

The editors is an instance method which is made automatically through the associations..refer to the Rails Guide for more information...

Upvotes: 0

Related Questions