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
#Books_controller - injecting the recommendation_id
@book_similars = Book.similars
@book_similars.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
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?