# Association generated methods not working

**URL:** <https://www.ruby-forum.com/t/association-generated-methods-not-working/185065>\
**Category:** Rails\
**Created:** [March 12, 2010, 8:43pm UTC](https://www.ruby-forum.com/t/association-generated-methods-not-working/185065 "2010-03-12T20:43:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjcharl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mjcharl](https://www.ruby-forum.com/u/mjcharl)\
**Post date:** [March 12, 2010, 8:43pm UTC](https://www.ruby-forum.com/t/association-generated-methods-not-working/185065/1 "2010-03-12T20:43:39Z")

</div>

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

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

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.

---

<div class="post-metadata">

**Author:** ![mjcharl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mjcharl](https://www.ruby-forum.com/u/mjcharl)\
**Post date:** [March 12, 2010, 9:43pm UTC](https://www.ruby-forum.com/t/association-generated-methods-not-working/185065/2 "2010-03-12T21:43:26Z")

</div>

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

On 12 March 2010 19:43, Martin C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![mjcharl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mjcharl](https://www.ruby-forum.com/u/mjcharl)\
**Post date:** [March 12, 2010, 9:50pm UTC](https://www.ruby-forum.com/t/association-generated-methods-not-working/185065/3 "2010-03-12T21:50:23Z")

</div>

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.
