Rails 3:通过关联包含来自 has_many 的字段

发布于 2024-12-31 21:20:14 字数 1752 浏览 0 评论 0原文

我的模型关联如下:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅冇旳回忆 2025-01-07 21:20:14

我建议您阅读有关关联的 Rails 指南,特别是关于 has_many :through协会

您的很多代码没有意义 - 例如:

@book_similars = Book.similars

这意味着您在 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:

@book_similars = Book.similars

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文