E
E
Evgeny Prokopiev2015-12-09 19:13:28
MySQL
Evgeny Prokopiev, 2015-12-09 19:13:28

How to get data on a condition linked to another table?

There are 3 models:

class Article < ActiveRecord::Base

  has_many :facts
  belongs_to :user
  has_and_belongs_to_many :users

end

class Fact < ActiveRecord::Base

  belongs_to :article
  belongs_to :user

end

class User < ActiveRecord::Base

  has_many :facts
  has_many :articles
  has_and_belongs_to_many :favorits, class_name: "Article"

end

How can I write a query to get all Facts owned by Articles that the User has marked as Favorit, only to have the creation date of the facts after the Article was added to Favotits? Thanks in advance)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2015-12-13
@Evgen174

In general, something like this:

@user.facts.
  joins(:favorits). # = articles_users ?
  where("facts.created_at > articles_users.created_at").
  all

I didn’t really understand why there are such connections between the models, so I could be mistaken.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question