Rails - HABTM Relationship — How Can I Find A Record Based On An Attribute Of The Associated Model

一个人想着一个人 提交于 2019-12-17 22:37:12

问题


I have setup this HABTM relationship in the past and its worked before....Now it isnt and I'm at my wits end trying to figure out whats wrong. I've looking through the rails guides all day and cant seem to figure out what I'm doing wrong, so help would really be appreciated.

I have 2 models connected through a join model and I'm trying to find records based an attribute of the associated model.

Event.rb

has_and_belongs_to_many :interests

Interest.rb

has_and_belongs_to_many :events

and a join table migration that was created like

  create_table 'events_interests', :id => false do |t|
      t.column :event_id, :integer
      t.column :interest_id, :integer
   end

I tried

 @events = Event.all(:include => :interest, :conditions => [" interest.id = ?", 4 ] )

But got the error "Association named 'interest' was not found; perhaps you misspelled it?"... which I didnt of course

I tried

  @events = Event.interests.find(:all, :conditions => [" interest.id = ?", 4 ] )

but got the error "undefined method `interests' for #Class:0x4383348"

How can I find the Events that have an interest id of 4....I'm definitely going bald from this lol


回答1:


You need to include the interests table.

@events = Event.all(:include => :interests, :conditions => ["interests.id = ?", 4])

You had it right in your post, but you didn't pluralize interests.

Update

Because this answer is still getting attention, I thought it might be a good idea to update it using ActiveRecord::Relation syntax since the above way is going to be deprecated.

@events = Event.includes(:interests).where(interests: { id: 4 })

or

@events = Event.includes(:interests).where('interests.id' => 4)

And in case you want a custom condition

@events = Event.includes(:interests).where('interests.id >= 4')



回答2:


The answer was helpful, thanks! I know this post is old, but I came up with a different solution that might be useful to someone. I noticed the query generate was quite long in my case. For me the table that was equatable to your "interests" table had a lot of columns and data in it. To avoid searching that table for the matching id, my solution looked similar to this:

@events = Event.all.where('events.id' => @interest.events.each(&:id)) 

This worked for me since I already had an instanced @interest object with the list of ids for the events. Of course you're not using some of the magic the rails has available. Also, I couldn't get your solution to work with "not". For example;

@events = Event.includes(:interests).where.not(interests: { id: 4 })

would not work. It would return 0 results. But this:

@events = Event.all.where.not('events.id' => @interest.events.each(&:id)) 

would work, which would return all the the events that don't have an association with @interest.



来源:https://stackoverflow.com/questions/4687004/rails-habtm-relationship-how-can-i-find-a-record-based-on-an-attribute-of-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!