Rails 4.2新手:
2个问题;
1)第一个has_many是否冗余?因为它的名字是Save Class的复数?
我只能拥有:
has_many :savers, through: :saves, source: :saver
甚至更好;
has_many :savers, through: :saves
如果答案是肯定的,我可以在哪里设置"dependent::destroy"?
class Post < ActiveRecord::Base
belongs_to :user
has_many :saves, class_name: "Save", foreign_key: "saver_id", dependent: :destroy
has_many :savers, through: :saves, source: :saver
end
class Save < ActiveRecord::Base
belongs_to :saver, class_name: "User"
validates :saver_id, presence: true
end
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
...
end
2)这是一个典型的博客模型,用户可以将另一个用户发布的帖子‘保存’到他们的时间线上。此模型是否使用了最佳实践?特别是在数据库性能方面,通过连接来获取用户保存的帖子。将有100 The行的“保存”表?
发布于 2016-02-07 20:06:22
首先,让我们稍微修改一下您的示例,以减少命名的混乱:
class User
has_many :bookmarks
has_many :posts, through: :bookmarks
end
class Post
has_many :bookmarks
has_many :users, through: :bookmarks
end
class Bookmark
belongs_to :user
belongs_to :post
end
让我们看一下在执行@user.posts
时生成的查询
irb(main):009:0> @user.posts
Post Load (0.2ms) SELECT "posts".* FROM "posts" INNER JOIN "bookmarks" ON "posts"."id" = "bookmarks"."post_id" WHERE "bookmarks"."user_id" = ? [["user_id", 1]]
=> #<ActiveRecord::Associations::CollectionProxy []>
现在让我们注释掉has_many :bookmarks
并重新加载:
class User
# has_many :bookmarks
has_many :posts, through: :bookmarks
end
irb(main):005:0> @user.posts
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :bookmarks in model User
所以不,第一个has_many
并不是多余的--事实上,它是has_many through:
工作原理的核心。您通过另一个关系设置了一个排序快捷方式。
请注意,在has_many :posts, through: :bookmarks
:bookmarks
中,我们正在连接的是名称关系。而不是包含联接的表。
要修复原始代码,您需要执行以下操作:
class Post < ActiveRecord::Base
has_many :saves, dependent: :destroy
has_many :savers, through: :saves
end
class Save < ActiveRecord::Base
belongs_to :saver, class_name: "User"
belongs_to :post # A join table with only one relation is pretty worthless.
validates :saver_id, presence: true
end
class User < ActiveRecord::Base
has_many :posts
has_many :saves, dependent: :destroy
has_many :posts, through: :saves
end
请注意,您不需要一半的垃圾-如果您有has_many :savers, through: :saves
,ActiveRecord将查找关系saver
本身。此外,您只想在连接模型上使用dependent: destroy
-而不是在post关系上,因为这会删除用户“保存”的所有帖子-甚至是其他人写的帖子!
发布于 2016-02-06 22:24:16
我自己教Rails,我想学习使用框架的专业方法和遵循Rails指南的最佳实践。这并不容易,因为我通常会找到“有效”的答案。
我会试着自己回答,也许这对Rails新手有用:
使用保存通过,关联,Rails首先通过查看<class>_id
形式的外键来推断关联,其中<class>
是类名的小写,在本例中为‘has_many _id’。
因此,如果我们有列名'save_id',我们将有以下简化的模型:
class Post < ActiveRecord::Base
belongs_to :user
has_many :saves, through: :saves
end
class Save < ActiveRecord::Base
belongs_to :savers, class_name: "User"
validates :save_id, presence: true
end
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
...
end
https://stackoverflow.com/questions/35231839
复制