経由にhas_manyからフィールドを含める私のモデルの関連付けがある:上記のようにRailsの3:次のように関連
#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
、本は多くのsimilars 勧告てを持っています。私の要件は、similarsを取得する間に、には、の対応テーブルの対応するレコードのidが含まれています。
私の質問は以下のとおりです。
にはどうすれば similarsを伴って含むフィールド*のrecommendation_id *を含めることができますか?
が直接含めることができない場合、何が(上記のように)
への正しい道を別々にこのフィールドを決定し、次いで I直接 それを使用できるようにsimilarsインスタンス変数に注入私の見解では?
ここで何をしようとしていますか?いくつかの詳細を追加すれば、より良いレスポンスが得られます。 –
@MatthewLehner、コメントありがとう。私はポストを編集して詳細を掲載しました。親切に助けてください。 – rgoraya
@ similar.recommendation_idは使えませんか? - それがうまくいかない場合は、似たようなモデルとあなたが覚えているビューロジックを投稿してください。 –