Rails 3: include a field from has_many through association

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?

On 30 January 2012 20:54, Raminder G. [email protected] wrote:

class Recommendation < ActiveRecord::Base
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?

You have not told us whether recomendation has_many similars or
belongs_to similar. The answer depends on that I think. Your
question may only make sense one way round but I would like
confirmation.

Colin

you have to do an extra loop to get “recommendation_id”
I think your @rec_id should be an array

I have had problems with has_many through in the past
personally I try to avoid it.