Answer the question
In order to leave comments, you need to log in
Has_many like belongs_to?
Hello!
The question is. There is a pages table and page_translations
class Page < ActiveRecord::Base
has_many :page_translations
end
class PageTranslation < ActiveRecord::Base
belongs_to :page
end
page_id | locale | title | content
@page = Page.joins(:page_translations).first
@page.title # так не могу вытащить title, который берется из таблицы page_translations, тк он вытащил не одну запись, как я понимаю
SELECT *
FROM pages p
JOIN page_translations pt
ON pt.page_id = p.id
WHERE p.parent_id = ?
AND p.is_published = ?
AND pt.locale = ?
ORDER BY p.lft DESC
Answer the question
In order to leave comments, you need to log in
Did it like this:
has_one :page_translation, -> (object){ where("locale = ?", I18n.locale) }
I don’t really understand why there are joins here, the id of the desired page is already known, isn’t it?
@page = PageTranslation.where(page_id: 123, locale: "en")
@page.title
@page.page_translations.first
, instead of first, any condition can be, as if we were making a request to the database
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question