I'm trying to get a grasp of how to work with associations in Rails, specifically, when and when not to write explicit SQL code.
In my application, I have four models, which are defined as follows:
class User < ActiveRecord::Base has_many :comments has_many :geographies has_many :communities, through: :geographies class Comment < ActiveRecord::Base belongs_to :user class Community < ActiveRecord::Base has_many :geographies has_many :users class Geography < ActiveRecord::Base belongs_to :user belongs_to :community
Users can post comments, and are associated to one or more communities through the geography table (the geography table stores
I have an index action listing all comments, and I would like to filter by community. Given a comment object, I can get the user object via
comment.user, but I can't chain beyond that (i.e., something like
comment.user.geography(0).community no funciona).
It seems this object chaining is a key feature of rails, but does it work with has_many :through associations? Given my example, is it possible to get the community object from the comment object by using object chaining, or would I need to write the SQL to get anything other than the user when given the comment object?
preguntado el 01 de julio de 12 a las 00:07
Since User is associated with multiple communities, you will need to tell ActiveRecord (or raw SQL) which community you want:
comment.user.communities #=> should give you all the communities
If you don't particularly care for getting all communities and just want to get any community
comment.user.communities.first #=> should give you the first community
But, generally you will be interested in one particular community, based on a condition.
comment.user.communities.where(name: 'Europe') #=> should give you the European community.
I don't think you need the geographies table.
class Community < ActiveRecord::Base has_many :users end class User < ActiveRecord::Base belongs_to :community has_many :comments end class Comment < ActiveRecord::Base belongs_to :user end
Then you can access a comment's user's community like