Josh Kestenberg
Josh Kestenberg

Reputation: 119

Can I directly query a join table to get the "created_at" date of an association?

I have a Member model and a Tag model. They have a many-to-many relationship established using activerecord associations. As a result, there is a members_tags simple join table reflected in my schema. This table by default has a created_at column. I wish to access this information to see the datetime at which a tag was applied to a member. Is this possible?

Upvotes: 0

Views: 543

Answers (1)

nhinhdao
nhinhdao

Reputation: 1

Since you have a many to many relationship between Member and Tag, you will have a connected model ModelTag or TagModel depends on how you named it). When you created the joined table model_tags (or tag_models) you will have access to the created_at column as well.

Let's say you have a ModelTag model. You can access any of its instance created_at column with, for example:

ModelTag.first.created_at

if you want to access the column created_at of all instances:

ModelTag.all.map {|mt| mt.created_at}

Dunno if it's what you want?

Upvotes: 0

Related Questions