Raminder G.
2012-Jan-30 20:54 UTC
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?
--
Posted via http://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2012-Jan-30 21:19 UTC
Re: Rails 3: include a field from has_many through association
On 30 January 2012 20:54, Raminder G. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Rolando Garro
2012-Jan-31 15:27 UTC
Re: Rails 3: include a field from has_many through association
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...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.