Ruby on Rails Tuesday, January 31, 2012

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.

On Jan 30, 2:54 pm, "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?
>
> 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?
>
> --
> Posted viahttp://www.ruby-forum.com/.

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment