I have an issue on filtering a has_many through association. My model is
as
follows
class Group
has_and_belongs_to_many :messages
has_many :tags, through :messages
end
class Message
has_and_belongs_to_many :tags
end
class Tag
has_and_belongs_to_many :messages
end
This gives me the tags for all messages in a group. Groups.first.tags
.
However I want to filter on an attribute in messages
(Message.where(:created => ‘11-11-2014’)) based on which tags should be
retrieved. Is it possible to do this in a has_many though association?
Thanks in advance
On 26 November 2014 at 08:40, Ganesh Ranganathan [email protected] wrote:
class Message
do this in a has_many though association? Thanks in advance
Groups.first.messages.where(:created => ‘11-11-2014’).tags
By the way, if you mean :created_at then this will not work as
created_at includes the time, so to get an exact match on 11-11-2014
it would have to be exactly midnight. You will have to use a time
range, and don’t forget to watch out for the timezone.
By the way, if you mean :created_at then this will not work as
created_at includes the time, so to get an exact match on 11-11-2014
it would have to be exactly midnight. You will have to use a time
range, and don’t forget to watch out for the timezone.
Hi Colin,
Thanks for your reply. This won’t work since messages would return an
ActiveRelation there is no tags method on an active relation.
In my actual code I will use a between two days. I just used a direct
equals for simplicity sake