【发布时间】:2015-01-30 10:30:20
【问题描述】:
这里是关联,
Publication : has_many exchange_rates、books 和 authors
Bookbelongs_to author, exchange_rate
Authorbelongs_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
【问题讨论】:
标签: mysql ruby-on-rails activerecord nested-queries