Rails 3:通过关联包含来自 has_many 的字段
我的模型关联如下:
#book model
class Book < ActiveRecord::Base
has_many :recommendations, :dependent => :destroy
has_many :similars, :through => :recommendations, :conditions => ['recommendation_type IS NULL'], :order => 'recommendations.created_at DESC'
#recommendation model
class Recommendation < ActiveRecord::Base
belongs_to :book
belongs_to :similar, :class_name => 'Book', :foreign_key => 'similar_id'
#Books_controller - injecting the recommendation_id
@book = Book.find(params[:id])
if params[:content_type]
@content_type = params[:content_type];
else
@content_type = "similars"
end
case @content_type
when "similars"
# get the similars
@book_content = @book.similars
@book_content.each do |similar|
@rec_id = Recommendation.where(:book_id=>similar.id, :recommendation_type=>'S').select('id').first.id
similar << {:rec_id => @rec_id}
# ^-- Above line gives NoMethodError (undefined method `<<' for #<Book:0x10de1f40>):
end
when "references"
# get the references
@book_content = @book.references
@book_content.each do |reference|
@rec_id = Recommendation.where(:book_id=>reference.id, :recommendation_type=>'R').select('id').first.id
reference << {:rec_id => @rec_id}
# ^-- Above line gives NoMethodError (undefined method `<<' for #<Book:0x10de1f40>):
end
end
因此,如上所述,一本书通过推荐有许多相似。我的要求是,在检索相似时,我还想在连接表推荐中包含相应记录的id。
我的问题是:
如何包含字段 *recommendation_id* 以及 相似?
如果不能直接包含,那么正确的方法是什么
单独确定这个字段(如上图)然后 将其注入到 similars 实例变量中,以便我可以使用 直接包含在我的视图中?
My model association is as follows:
#book model
class Book < ActiveRecord::Base
has_many :recommendations, :dependent => :destroy
has_many :similars, :through => :recommendations, :conditions => ['recommendation_type IS NULL'], :order => 'recommendations.created_at DESC'
#recommendation model
class Recommendation < ActiveRecord::Base
belongs_to :book
belongs_to :similar, :class_name => 'Book', :foreign_key => 'similar_id'
#Books_controller - injecting the recommendation_id
@book = Book.find(params[:id])
if params[:content_type]
@content_type = params[:content_type];
else
@content_type = "similars"
end
case @content_type
when "similars"
# get the similars
@book_content = @book.similars
@book_content.each do |similar|
@rec_id = Recommendation.where(:book_id=>similar.id, :recommendation_type=>'S').select('id').first.id
similar << {:rec_id => @rec_id}
# ^-- Above line gives NoMethodError (undefined method `<<' for #<Book:0x10de1f40>):
end
when "references"
# get the references
@book_content = @book.references
@book_content.each do |reference|
@rec_id = Recommendation.where(:book_id=>reference.id, :recommendation_type=>'R').select('id').first.id
reference << {:rec_id => @rec_id}
# ^-- Above line gives NoMethodError (undefined method `<<' for #<Book:0x10de1f40>):
end
end
So as noted above, A book has many similars through recommendations. My requirement is that while retrieving similars, I would also like to include the id of the corresponding record in the join table recommendations.
My questions are:
How can I include the field *recommendation_id* alongwith
similars?If it cannot be included directly, then what is the correct way to
determine this field separately (as shown above) and then
inject it into the similars instance variable so that I can use
it directly in my views?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您阅读有关关联的 Rails 指南,特别是关于 has_many :through协会。
您的很多代码没有意义 - 例如:
这意味着您在
Book
模型上有一个类似的类方法,但您没有提到它的定义或它返回的内容。 Rails 不是这样工作的。I recommend you read the Rails guide on associations, specifically about the has_many :through associations.
A lot of your code doesn't make sense - for example:
This means you have a class method on the
Book
model for similars, but you don't mention it being defined, or what it returns. Rails just doesn't work like this.