# Problem with has\_many :through Association

**URL:** <https://discuss.rubyonrails.org/t/problem-with-has-many-through-association/48095>\
**Category:** rubyonrails-talk\
**Created:** [December 20, 2009, 6:32pm UTC](https://discuss.rubyonrails.org/t/problem-with-has-many-through-association/48095 "2009-12-20T18:32:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeonS](https://avatars.discourse-cdn.com/v4/letter/l/c77e96/32.png) [@LeonS](https://discuss.rubyonrails.org/u/LeonS)\
**Post date:** [December 20, 2009, 6:32pm UTC](https://discuss.rubyonrails.org/t/problem-with-has-many-through-association/48095/1 "2009-12-20T18:32:29Z")

</div>

Hi, I've got a problem with a has\_many :through association, i cant call the u1.UsersProfileAttributes.find\_by\_ProfileAttribute\_name ("icq") method, rails means that this method doesn't exist. The method u1.UsersProfileAttributes.find\_by\_ProfileAttribute\_id(3) works correctly. u1 is a user object. I don't know whats the problem, because my associations seems to be okay.

Have a look:

class ProfileAttribute \< ActiveRecord::Base &nbsp;&nbsp;has\_many :UsersProfileAttributes &nbsp;&nbsp;has\_many :users, :through =\> :UsersProfileAttributes end class User \< ActiveRecord::Base &nbsp;&nbsp;has\_many :UsersProfileAttributes &nbsp;&nbsp;has\_many :ProfileAttributes, :through =\> :UsersProfileAttributes end class UsersProfileAttribute \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :user &nbsp;&nbsp;belongs\_to :ProfileAttribute end

My Migration file:

&nbsp;&nbsp;&nbsp;&nbsp;class CreateProfileAttributes \< ActiveRecord::Migration &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.up &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create\_table :profile\_attributes do |t| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.string :name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.integer :profile\_group\_id &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.timestamps &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;create\_table :users\_profile\_attributes do |t| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.integer :user\_id &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.integer :ProfileAttribute\_id &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.string :value &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end &nbsp;&nbsp;def self.down &nbsp;&nbsp;&nbsp;&nbsp;drop\_table :profile\_attributes &nbsp;&nbsp;&nbsp;&nbsp;drop\_table :users\_profile\_attributes &nbsp;&nbsp;end end

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [December 20, 2009, 11:03pm UTC](https://discuss.rubyonrails.org/t/problem-with-has-many-through-association/48095/2 "2009-12-20T23:03:02Z")

</div>

> Hi, I've got a problem with a has\_many :through association, i cant call the u1.UsersProfileAttributes.find\_by\_ProfileAttribute\_name ("icq") method, rails means that this method doesn't exist. The method u1.UsersProfileAttributes.find\_by\_ProfileAttribute\_id(3) works correctly. u1 is a user object. I don't know whats the problem, because my associations seems to be okay.

has\_many :UserProfileAttributes etc... should really be has\_many :user\_profile\_attributes. Looks like you're getting away with it for now but you're probably storing up trouble for now.

u1.UsersProfileAttributes.find\_by\_ProfileAttribute\_name doesn't work because the users\_profile\_attributes tables doesn't have a name column ( but it does have a ProfileAttribute\_id so your second example does work).

Fred
