HABTM 关系——如何根据关联模型的属性查找记录
我过去已经设置过这种 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要包括兴趣表。
你的帖子里说得很对,但你没有多元化的兴趣。
更新
因为这个答案仍然受到关注,所以我认为使用
ActiveRecord::Relation
语法更新它可能是一个好主意,因为上述方式将被弃用。或者
如果您想要自定义条件
You need to include the interests table.
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.or
And in case you want a custom condition
答案很有帮助,谢谢!我知道这篇文章很旧,但我想出了一个可能对某人有用的不同解决方案。我注意到在我的例子中生成的查询相当长。对我来说,相当于您的“兴趣”表的表中有很多列和数据。为了避免在该表中搜索匹配的 id,我的解决方案看起来与此类似:
这对我有用,因为我已经有一个实例化的 @interest 对象,其中包含事件的 id 列表。当然,您并没有使用 Rails 提供的一些魔力。另外,我无法让你的解决方案与“不”一起使用。例如;
行不通。它将返回 0 个结果。但这:
会起作用,它将返回与@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:
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;
would not work. It would return 0 results. But this:
would work, which would return all the the events that don't have an association with @interest.