Association generated methods not working

I’m definitely a noob to rails so sorry if this is trivial. I have
tables with associations on them. The associations are supposed to add
methods to my models like model.others and model.others= and so on but
when I try to use them I get errors like the following.

NoMethodError: undefined method states' for #<Ad:0x2c8a88c> from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/attribute_methods.rb:260:in method_missing’
from (irb):4

I’ve followed the execution in the debugger and all that the
method_missing seems to do is generate setters, getters and ? for the
fields in the table, it doesn’t seem to generate or look for any of the
methods that

says it should. I only did this once though and I didn’t really
understand what I was reading

My code is the following

Model classes:

file: app/models/ad.rb
class Ad < ActiveRecord::Base
belongs_to :customer
has_and_belongs_to_many :state
end

file: app/models/state.rb
class State < ActiveRecord::Base
has_and_belongs_to_many :ad
has_many :zip_code
has_many :town
end

Migration:

create_table :ads do |t|
t.integer :property_type_id
t.float :priority_weighting
t.float :price_min
t.float :price_max
t.float :distance_max
t.boolean :for_sale
t.boolean :for_lease
t.float :cost_per_click
t.float :cost_per_click
t.references :customer
t.binary :image_size_1
t.binary :image_size_2

  t.timestamps
end

create_table “states”, :force => true do |t|
t.string “name”
t.string “abbreviation”
end

create_table “ads_states”, :force => true do |t|
t.integer “ad_id”
t.integer “state_id”
end

(actually I took this from schema.rb)

when I run script/console and play around I get this:

state = State.new
=> #<State id: nil, name: nil, abbreviation: nil>
state.save
=> true
ad = Ad.new
=> #<Ad id: nil, property_type_id: nil, priority_weighting: nil,
price_min: nil, price_max: nil, distance_max: nil, for_sale: nil,
for_lease: nil, cost_per_click: nil, customer_id: nil, image_size_1:
nil, image_size_2: nil, created_at: nil, updated_at: nil>
ad.state
=> []
ad.state_ids
=> []
ad.state_ids = [state.id]
=> [2]
ad.states
NoMethodError: undefined method states' for #<Ad:0x419db84> from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/attribute_methods.rb:260:in method_missing’
from (irb):20
ad.states = [state]
NoMethodError: undefined method states=' for #<Ad:0x419db84> from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/attribute_methods.rb:255:in method_missing’
from (irb):21
ad.states << [state]
NoMethodError: undefined method states' for #<Ad:0x419db84> from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/attribute_methods.rb:260:in method_missing’
from (irb):22

I have similar problems with has_many however belongs_to seems to work.

I get the same exact problems when I try this in the controller.
I’m using Rails 2.3.5 and Ruby 1.8.7

Please let me know if you need any more info this is all I could think
of.

Following just a quick look at the code see below for some obvious
issues.

On 12 March 2010 19:43, Martin C. [email protected] wrote:

Model classes:

file: app/models/ad.rb
class Ad < ActiveRecord::Base
belongs_to :customer
has_and_belongs_to_many :state

That should be :states (plural)

end

file: app/models/state.rb
class State < ActiveRecord::Base
has_and_belongs_to_many :ad

Should be :ads

has_many :zip_code

:zip_codes

has_many :town

:towns

Colin

Thank you so much. That solved it. I can’t believe I didn’t try
changing that. I could have sworn singular was the way it was done in
the guides and tutorials. knowing when to use plural and when to use
singular is confusing. actually when you think about it it always makes
sense but for some reason it just doesn’t click with me.