以下是关联,
Publication
:has_many exchange_rates
,books
和authors
Book
belongs_to author
,exchange_rate
Author
belongs_to exchange_rate
我正在尝试编写一个查询,以便在books
之后找到computed_column
。
这是我到目前为止尝试的查询
publication.books.joins(:exchange_rate, :author).select("books.*, (exchange_rates.exchange_ratio + (SELECT exchange_rates.exchange_ratio FROM exchange_rates WHERE exchange_rate_id = authors.exchange_rate_id)) AS computed_column")
我的目标是生成一个根据关联表属性计算的列。
这意味着,
book.exchange_rate.exchange_ratio + book.author.exchange_rate.exchange_ration AS computed_column
答案 0 :(得分:0)
在computed_column
attr_accessor
作为Book
class Book
attr_accessor :computed_column
end
然后,查询中的Book
每个实例都会book.computed_column
可用