0

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
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
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.

Thank you

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by colwem
0

Never mind I solved it. has_many and has_and_belongs_to_many require plural table names. That was my problem. I noticed that this forum allows you to mark a problem solved but I can't figure out how to do that.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.