# Problem with has\_many :through Association

**URL:** <https://www.ruby-forum.com/t/problem-with-has-many-through-association/180514>\
**Category:** Rails\
**Created:** [December 21, 2009, 12:00am UTC](https://www.ruby-forum.com/t/problem-with-has-many-through-association/180514 "2009-12-21T00:00:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeonS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@LeonS](https://www.ruby-forum.com/u/LeonS)\
**Post date:** [December 21, 2009, 12:00am UTC](https://www.ruby-forum.com/t/problem-with-has-many-through-association/180514/1 "2009-12-21T00:00:41Z")

</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  
has\_many :UsersProfileAttributes  
has\_many :users, :through =\> :UsersProfileAttributes  
end  
class User \< ActiveRecord::Base  
has\_many :UsersProfileAttributes  
has\_many :ProfileAttributes, :through =\> :UsersProfileAttributes  
end  
class UsersProfileAttribute \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :ProfileAttribute  
end

My Migration file:

```
class CreateProfileAttributes < ActiveRecord::Migration
  def self.up
    create_table :profile_attributes do |t|
      t.string :name
      t.integer :profile_group_id
      t.timestamps
    end
create_table :users_profile_attributes do |t|
  t.integer :user_id
  t.integer :ProfileAttribute_id
  t.string :value
end

```

end  
def self.down  
drop\_table :profile\_attributes  
drop\_table :users\_profile\_attributes  
end  
end

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![LeonS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@LeonS](https://www.ruby-forum.com/u/LeonS)\
**Post date:** [December 21, 2009, 12:04am UTC](https://www.ruby-forum.com/t/problem-with-has-many-through-association/180514/2 "2009-12-21T00:04:12Z")

</div>

On Dec 20, 6:32 pm, LeonS [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

> class UsersProfileAttribute \< ActiveRecord::Base  
> t.integer :profile\_group\_id  
> drop\_table :users\_profile\_attributes  
> end  
> end

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
