HABTM 关系——如何根据关联模型的属性查找记录

发布于 2024-10-12 06:25:55 字数 972 浏览 4 评论 0原文

我过去已经设置过这种 HABTM 关系,并且以前一直有效......现在不行,我束手无策,试图找出问题所在。我一整天都在浏览 Rails 指南,似乎无法弄清楚我做错了什么,所以非常感谢您的帮助。

我有 2 个模型通过连接模型连接,我试图根据关联模型的属性查找记录。

Event.rb

has_and_belongs_to_many :interests

Interest.rb

has_and_belongs_to_many :events

和创建的连接表迁移如下:

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

我尝试过:

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

但收到错误:

“未找到名为“interest”的关联;也许您拼写错误?”...

我当然没有错。

我尝试过:

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

但收到错误:

“#Class:0x4383348 的未定义方法‘兴趣’”

我怎样才能找到兴趣 id 为 4 的事件....我肯定会因为这个哈哈而秃头

I have setup this HABTM relationship in the past and it has worked before....Now it isn't and I'm at my wits end trying to figure out what's wrong. I've looked through the rails guides all day and can't 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 didn't off 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森林迷了鹿 2024-10-19 06:25:55

您需要包括兴趣表。

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

你的帖子里说得很对,但你没有多元化的兴趣。

更新

因为这个答案仍然受到关注,所以我认为使用 ActiveRecord::Relation 语法更新它可能是一个好主意,因为上述方式将被弃用。

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

或者

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

如果您想要自定义条件

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

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')
与他有关 2024-10-19 06:25:55

答案很有帮助,谢谢!我知道这篇文章很旧,但我想出了一个可能对某人有用的不同解决方案。我注意到在我的例子中生成的查询相当长。对我来说,相当于您的“兴趣”表的表中有很多列和数据。为了避免在该表中搜索匹配的 id,我的解决方案看起来与此类似:

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

这对我有用,因为我已经有一个实例化的 @interest 对象,其中包含事件的 id 列表。当然,您并没有使用 Rails 提供的一些魔力。另外,我无法让你的解决方案与“不”一起使用。例如;

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

行不通。它将返回 0 个结果。但这:

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

会起作用,它将返回与@interest没有关联的所有事件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文