Ruby on rails 你自己也经历过一些事情吗
所以,我有一个系统,用户可以跟随作者(其他用户) 用户型号:Ruby on rails 你自己也经历过一些事情吗,ruby-on-rails,ruby-on-rails-3,activerecord,Ruby On Rails,Ruby On Rails 3,Activerecord,所以,我有一个系统,用户可以跟随作者(其他用户) 用户型号: class User < ActiveRecord::Base has_many :author_following, class_name: 'Following' has_many :following, through: :author_following, source: :author has_many :followers, foreign_key: 'author_id', throug
class User < ActiveRecord::Base
has_many :author_following, class_name: 'Following'
has_many :following, through: :author_following, source: :author
has_many :followers, foreign_key: 'author_id', through: :author_following, source: :user
end
class Following < ActiveRecord::Base
belongs_to :user
belongs_to :author, foreign_key: 'author_id', class_name: "User"
end
class User < ActiveRecord::Base
has_many :author_following, class_name: 'Following'
has_many :following, through: :author_following, source: :author
has_many :author_followers, foreign_key: 'author_id', class_name: 'Following'
has_many :followers, through: :author_followers, source: :user
end
class Following < ActiveRecord::Base
belongs_to :user
belongs_to :author, class_name: "User"
end
这是正确的
u.followers
生成以下SQL:
SELECT "users".* FROM "users" INNER JOIN "followings" ON "users"."id" = "followings"."user_id" WHERE "followings"."user_id" = $1 [["user_id", 1]]
这是错误的
理想情况下这个SQL应该是
其中的“followers”。“author_id”=$1
当然,在发布问题后,我认为这是您的权利。但是,如果您认为有一种更优雅的方法可以做到这一点,请评论:)
为了解决这个问题,我改变了:
用户型号:
class User < ActiveRecord::Base
has_many :author_following, class_name: 'Following'
has_many :following, through: :author_following, source: :author
has_many :followers, foreign_key: 'author_id', through: :author_following, source: :user
end
class Following < ActiveRecord::Base
belongs_to :user
belongs_to :author, foreign_key: 'author_id', class_name: "User"
end
class User < ActiveRecord::Base
has_many :author_following, class_name: 'Following'
has_many :following, through: :author_following, source: :author
has_many :author_followers, foreign_key: 'author_id', class_name: 'Following'
has_many :followers, through: :author_followers, source: :user
end
class Following < ActiveRecord::Base
belongs_to :user
belongs_to :author, class_name: "User"
end
class用户
以下型号:
class User < ActiveRecord::Base
has_many :author_following, class_name: 'Following'
has_many :following, through: :author_following, source: :author
has_many :followers, foreign_key: 'author_id', through: :author_following, source: :user
end
class Following < ActiveRecord::Base
belongs_to :user
belongs_to :author, foreign_key: 'author_id', class_name: "User"
end
class User < ActiveRecord::Base
has_many :author_following, class_name: 'Following'
has_many :following, through: :author_following, source: :author
has_many :author_followers, foreign_key: 'author_id', class_name: 'Following'
has_many :followers, through: :author_followers, source: :user
end
class Following < ActiveRecord::Base
belongs_to :user
belongs_to :author, class_name: "User"
end
类后面的
另一种方法是使用has\u和\u属于\u many
。不需要第二个模型
class User < ActiveRecord::Base
has_and_belongs_to_many :followers, class_name: 'User', foreign_key: 'follower_id'
has_and_belongs_to_many :followees, class_name: 'User', foreign_key: 'followee_id'
end
# Migration
create_table :followees_followers do |t|
t.belongs_to :followee
t.belongs_to :follower
end
class用户
这很简单,但是验证部分(比如验证某人是作者)需要在用户模型中完成@上面Billy Chan的答案很接近,但是您还需要指定关系的另一面以及“关联外键”,并在我们这边将followee\u id与followee\u id切换。此外,联接表实际上是用户
class User < ActiveRecord::Base
has_and_belongs_to_many :followers, class_name: 'User',
foreign_key: 'followee_id', association_foreign_key: 'follower_id'
has_and_belongs_to_many :followees, class_name: 'User',
foreign_key: 'follower_id', association_foreign_key: 'followee_id'
end
# Migration
create_table :users_users do |t|
t.belongs_to :followee
t.belongs_to :follower
end
class用户
现在User.followers和User.followeres按预期工作非常简单。我喜欢它:D@MichaelLynch,我的荣幸。从理论上讲,它应该可以工作,虽然没有经过验证:)这不会工作,rails会在另一种关系上寻找用户id。6年后,你救了我一天,想不出更好的方法用rails 6实现这一点