Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 通过自我指涉联想有很多_Ruby On Rails_Associations_Has Many Through_Self Reference - Fatal编程技术网

Ruby on rails 通过自我指涉联想有很多

Ruby on rails 通过自我指涉联想有很多,ruby-on-rails,associations,has-many-through,self-reference,Ruby On Rails,Associations,Has Many Through,Self Reference,作为一个例子,我想创建一个has\u many关联到一个人的朋友发布的所有帖子,比如has\u many:remote\u posts给我一些类似person>friends>person>posts的东西 …我会这样做的 script/generate model post title:string person_id:integer script/generate model friendship person_id:integer friend_id:integer script/gene

作为一个例子,我想创建一个has\u many关联到一个人的朋友发布的所有帖子,比如has\u many:remote\u posts给我一些类似person>friends>person>posts的东西

…我会这样做的

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
现在,

…我得到了这个错误

ActiveRecord::StatementInvalid:SQLite3::SQLException:没有这样的列:people.person\u id:选择posts.*从posts内部加入posts.person\u id=people.id,其中people.person\u id=1

除了外键错误之外,似乎友谊协会根本没有发挥作用。我想这可能是因为:source=>:posts,因为posts协会会加入两次

我可以编写一些finder sql,这就是我目前正在做的工作,尽管我更愿意这样做

有什么办法让它工作吗?

这个怎么样:

在友谊类中,添加:

has_many :posts, :through => :person
在Person类中,将远程发布更改为:

has_many :remote_posts, :class_name => 'Post',
         :through => :friendships, :source => :person
一个嵌套的has_多:通过关系。这似乎对我有用:

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

注意:这需要这个。注意:与github回购协议的直接链接似乎已中断。。。但是,尽管有错误消息,回购协议仍然存在。

JRL,这更好!不幸的是,它仍然无法完全工作,可能是因为在您调用:Person.find_by_name'frank.remote_posts.时出现了一个不相关的问题。它会触发以下SQL:SELECT posts.*FROM posts-internal-JOIN-friends-ON-posts.id=friends.Person\u-id WHERE-friends.friends\u-id=2它应该在posts.Person\u-id上加入,而不是posts.id,所以它返回的帖子的id与我尝试输入的朋友的id相同:foreign_key,但似乎不起作用。谢谢你的回复!该死-我早该知道它格式不正确。。问题是personremote_posts使用post.id而不是post.person_id,但我认为这是80%的情况!
has_many :posts, :through => :person
has_many :remote_posts, :class_name => 'Post',
         :through => :friendships, :source => :person
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