Say if @news_writers is an array of records. I then want to use @news_writers to find all news items that are written by all the news writers contained in @news_writers.
假设@news_writers是一个记录数组。然后,我想使用@news_writers查找由@news_writers中包含的所有新闻作者编写的所有新闻项目。
So I want something like this (but this is syntactically incorrect):
所以我想要这样的东西(但这在语法上是不正确的):
@news = News.find_all_by_role_id(@news_writers.id)
Note that
class Role < ActiveRecord::Base
has_many :news
end
and
class News < ActiveRecord::Base
belongs_to :role
end
2 个解决方案
#1
Like ennen, I'm unsure what relationships your models are supposed to have. But in general, you can find all models with a column value from a given set like this:
和ennen一样,我不确定你的模型应该有什么样的关系。但一般来说,您可以找到具有给定集合中列值的所有模型,如下所示:
News.all(:conditions => {:role_id => @news_writers.map(&:id)})
This will create a SQL query with a where condition like:
这将创建一个SQL查询,其中where条件如下:
WHERE role_id IN (1, 10, 13, ...)
where the integers are the ids of the @news_writers.
其中整数是@news_writers的id。
#2
I'm not sure if I understand you - @news_writers is a collection of Role models? If that assumption is correct, your association appears to be backwards - if these represent authors of news items, shouldn't News belong_to Role (being the author)?
我不确定我是否理解你 - @news_writers是一个角色模型的集合?如果这个假设是正确的,那么你的关联似乎是倒退 - 如果这些代表新闻项目的作者,那么新闻属于哪个角色(作为作者)?
At any rate, I would assume the most direct approach would be to use an iterator over @news_writers, calling on the association for each news_writer (like news_writer.news) in turn and pushing it into a separate variable.
无论如何,我认为最直接的方法是在@news_writers上使用迭代器,依次调用每个news_writer(如news_writer.news)的关联并将其推送到一个单独的变量中。
Edit: Daniel Lucraft's suggestion is a much more elegant solution than the above.
编辑:Daniel Lucraft的建议是比上述更优雅的解决方案。
#1
Like ennen, I'm unsure what relationships your models are supposed to have. But in general, you can find all models with a column value from a given set like this:
和ennen一样,我不确定你的模型应该有什么样的关系。但一般来说,您可以找到具有给定集合中列值的所有模型,如下所示:
News.all(:conditions => {:role_id => @news_writers.map(&:id)})
This will create a SQL query with a where condition like:
这将创建一个SQL查询,其中where条件如下:
WHERE role_id IN (1, 10, 13, ...)
where the integers are the ids of the @news_writers.
其中整数是@news_writers的id。
#2
I'm not sure if I understand you - @news_writers is a collection of Role models? If that assumption is correct, your association appears to be backwards - if these represent authors of news items, shouldn't News belong_to Role (being the author)?
我不确定我是否理解你 - @news_writers是一个角色模型的集合?如果这个假设是正确的,那么你的关联似乎是倒退 - 如果这些代表新闻项目的作者,那么新闻属于哪个角色(作为作者)?
At any rate, I would assume the most direct approach would be to use an iterator over @news_writers, calling on the association for each news_writer (like news_writer.news) in turn and pushing it into a separate variable.
无论如何,我认为最直接的方法是在@news_writers上使用迭代器,依次调用每个news_writer(如news_writer.news)的关联并将其推送到一个单独的变量中。
Edit: Daniel Lucraft's suggestion is a much more elegant solution than the above.
编辑:Daniel Lucraft的建议是比上述更优雅的解决方案。