Elegantly Selecting Attributes from Has_Many :Through Join Models in Rails

Access extra attributes in join table when using though in has_many relationship

In your User model, you'll have something like:

class User < ActiveRecord::Base
has_many :groupizations
has_many :groups, :through => :groupizations
end

You'd access the role information just through the has_many relationship to groupizations like this in the console.

foo = User.first
bar = foo.groupizations.first
bar.role

Assuming you had groupizations on the first user.

Getting a specific user/group relationship could be done like this:

Groupizations.where("group_id = ? and user_id = ?", group.id, user.id)

Then from there you could get the role you were looking for.

Need data from rails join table, has_many :through

To do this you need to use a bit of SQL in the has_many. Something like this should hopefully work.
has_many :users, :through => :follows, :select => 'users.*, follows.is_admin as is_follow_admin'

Then in the loop you should have access to
user.is_follow_admin

using join model attributes in rails has_many :through

solved with

class Group
has_many :groups_users
has_many :moderatorships, :class_name => "GroupsUser",:conditions => {:moderator => true}
has_many :moderators, :through => :moderatorships, :class_name => "User", :source => :user
end

Preferred to Matt van Horn's answer because this one produces only one query when we select user info with @group.moderators, while his solution gives separate query for each moderator

edit: updated to answer Sizzlepants' question. moderators created with this code should have moderator attribute in join model set to true (rails uses :conditions while creating join model), also, if i'd have to code it now, i'd name groups_users memberships for readability.

Get attribute value from the join in a many-to-many relationship

You could access it through User since it's a has_many ... :through relation:

User.first.link_addresses.first.address

Or, if you'd like to go through links then:

User.first.links.first.link_addresses.first.address

How should I approach has many through relationships with Single Table Inheritance (STI) in Rails 4.0

This is a challenging case for ActiveRecord. It needs to infer that columns in the self-join needed to find districts are STI instances. Apparently it's not smart enough to get this right. Since the only table is places, it's not much of a surprise that it generates this query:

SELECT "places".* FROM "places" 
INNER JOIN "places" "cities_districts_join"
ON "places"."parent_id" = "cities_districts_join"."id"
WHERE "places"."type" IN ('City') <<<<< ERROR HERE
AND "places"."type" IN ('District')
AND "cities_districts_join"."parent_id" = ?

As you can see the type check must fail since one string can't be both City and District. All would work if the first clause in the WHERE were instead

WHERE "cities_districts_join"."type" IN ('City')  

I tried several options on the relations (thought :class_name might do it), but no joy.

You can work around this limitation with SQL. Delete the has_many ... through in the Country class and replace with

def districts
District.find_by_sql(['SELECT * from places AS city
INNER JOIN places AS district
ON district.parent_id = city.id
WHERE city.parent_id = ?', id])
end

Or maybe someone else will see a more elegant way. If not, you might consider posting this as an issue in Rails development. It's an interesting case.



Related Topics



Leave a reply



Submit