On 30 January 2012 20:54, Raminder G. <li...@ruby-forum.com> wrote: > 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?
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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.