has_many 通过自引用关联

发布于 2024-08-08 12:50:22 字数 1863 浏览 13 评论 0原文

我想(作为示例)为某个人的朋友的所有帖子创建一个 has_many 关联,例如 has_many :remote_posts 给我类似 person > 的内容;朋友>人>帖子

要采取的方法

script/generate model post title:string person_id:integer
script/generate model friendship person_id:integer friend_id:integer
script/generate model person name:string

class Person < ActiveRecord::Base
  has_many :posts
  has_many :friendships, :foreign_key => 'friend_id'
  has_many :people, :through => :friendships
  has_many :remote_posts, :class_name => 'Post', :through => :people, :source => :posts
end
class Friendship < ActiveRecord::Base
  belongs_to :person
  #also has a 'friend_id' to see who the friendship is aimed at
end
class Post < ActiveRecord::Base
  belongs_to :person
end

# generate some people and friends
{'frank' => ['bob','phil'], 'bob' => ['phil']}.each {|k,v|
  v.each {|f| 
    Friendship.create(
      :person_id => Person.find_or_create_by_name(f).id,
      :friend_id => Person.find_or_create_by_name(k).id
    )
  }
}
# generate some posts
Person.all.each {|p|
  p.posts.create({:title => "Post by #{p.name}"})
}

..这是我现在

Person.first.friendships  # ..works
Person.first.people  # (friends) ..works
Person.first.posts # ..works
Person.first.remote_posts #....

, ...我收到此错误..

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: people.person_id: SELECT "posts".* FROM "posts" INNER JOIN "people" ON "posts".person_id = "people".id WHERE (("people".person_id = 1))

除了外键错误之外 - 似乎友谊关联不是'根本没有发挥作用。我在想这可能是因为 :source =>; :posts,因为 posts 关联会出现两次。

我可以编写一些 finder sql(这就是我目前正在做的事情),尽管我更愿意这样做。

关于如何让它发挥作用有什么想法吗?

I want to (as an example) create a has_many association to all posts by friends of a person, something like has_many :remote_posts to give me something like person > friends > person > posts.

..here is how I would go about it

script/generate model post title:string person_id:integer
script/generate model friendship person_id:integer friend_id:integer
script/generate model person name:string

class Person < ActiveRecord::Base
  has_many :posts
  has_many :friendships, :foreign_key => 'friend_id'
  has_many :people, :through => :friendships
  has_many :remote_posts, :class_name => 'Post', :through => :people, :source => :posts
end
class Friendship < ActiveRecord::Base
  belongs_to :person
  #also has a 'friend_id' to see who the friendship is aimed at
end
class Post < ActiveRecord::Base
  belongs_to :person
end

# generate some people and friends
{'frank' => ['bob','phil'], 'bob' => ['phil']}.each {|k,v|
  v.each {|f| 
    Friendship.create(
      :person_id => Person.find_or_create_by_name(f).id,
      :friend_id => Person.find_or_create_by_name(k).id
    )
  }
}
# generate some posts
Person.all.each {|p|
  p.posts.create({:title => "Post by #{p.name}"})
}

Now,

Person.first.friendships  # ..works
Person.first.people  # (friends) ..works
Person.first.posts # ..works
Person.first.remote_posts #....

...and I get this error..

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: people.person_id: SELECT "posts".* FROM "posts" INNER JOIN "people" ON "posts".person_id = "people".id WHERE (("people".person_id = 1))

Aside from the foreign key error - seems like the friendships association isn't coming into play at all. I was thinking that this might be because of the :source => :posts, since the posts association would come into it twice.

I could write some finder sql (and that is what I have working at the moment), though I'd sooner do it this way.

Any ideas of how to get this to work?

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

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

发布评论

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

评论(2

断爱 2024-08-15 12:50:22

怎么样:

FriendShip 类中,添加:

has_many :posts, :through => :person

并在 Person 类中,将remote_posts 更改为:

has_many :remote_posts, :class_name => 'Post',
         :through => :friendships, :source => :person

How about this:

In the FriendShip class, add:

has_many :posts, :through => :person

and in the Person class, change the remote_posts to:

has_many :remote_posts, :class_name => 'Post',
         :through => :friendships, :source => :person
欢你一世 2024-08-15 12:50:22

嵌套 has_many :through 关系怎么样。这似乎对我有用:

class Friendship < ActiveRecord::Base
  belongs_to :person
  belongs_to :friend, :class_name => 'Person'
  has_many :posts, :through => :friend, :source => :posts
end
class Person < ActiveRecord::Base
  has_many :posts
  has_many :friendships, :foreign_key => 'friend_id'
  has_many :people, :through => :friendships
  has_many :remote_posts, :through => :friendships, :source => :posts
end

注意:这需要这个 nested_has_many_through 插件。 (注意:直接链接到 github 存储库似乎已损坏......但尽管出现错误消息,该存储库仍然存在。)

How about a nested has_many :through relationship. This seems to work for me:

class Friendship < ActiveRecord::Base
  belongs_to :person
  belongs_to :friend, :class_name => 'Person'
  has_many :posts, :through => :friend, :source => :posts
end
class Person < ActiveRecord::Base
  has_many :posts
  has_many :friendships, :foreign_key => 'friend_id'
  has_many :people, :through => :friendships
  has_many :remote_posts, :through => :friendships, :source => :posts
end

Note: this requires this nested_has_many_through plugin. (Note: direct linking to github repos seems to be broken... but that repo is there despite the error message.)

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