Rails有许多和属于 [英] Rails has many and belongs to
问题描述
在M. Hartly的教程中,我了解了模型及其关系的概念(微博belongs_to用户,以及用户has_many微博)。
然而,我不确定如何在日常交易项目中付诸实践。
我决定为拥有不同权利的两种用户类型的用户建立一个模型: b admin_user谁可以创建/更新/编辑/删除交易 可以看到交易的standard_user以及他们是否有兴趣参与交易
$ b
两位用户都进入用户数据库
我也决定拥有一个交易模型,并进入交易数据库
所以我理解得很好:
-
a admin_user has_many deals
-
交易属于admin_user
-
standard_user有很多交易(事实上他可以参与在很多交易中都是如此)
-
并且交易不属于standard_user,因此我无法在此处说出任何内容。
我的理解是正确的吗?可以根据用户的权利类型来定义与模型对象的不同关系(在这种情况下为交易):
PS:我想我会分开使用cancan gem的用户可以给他们不同的权限
您所描述的最大问题是standard_user has_many:交易
,但交易不属于standard_user;他们可能同时被许多标准用户使用。这意味着交易不能 belongs_to
标准用户,因为您的交易中不能有 standard_user_id
>列表(因为您需要任意多列来处理可能参与的任意多用户)。
为了获得像这样的多对多关系,你需要一个链接表。这里有一个方法可以在Rails中实现:
class DealParticipation< ActiveRecord:Base
#这意味着deal_participations表有一个standard_user_id键
belongs_to:standard_user
#这意味着deal_participations表有一个deal_id键
belongs_to:deal
#...更多的逻辑去这里...
end
现在你的标准用户看起来像:
class StandardUser< ActiveRecord :: Base
has_many:deal_participations
has_many:deals,:through => :deal_participations
#... more logic goes here ...
end
您的交易类看起来像:
class Deal< ActiveRecord :: Base
has_many:deal_participations
has_many:standard_users,:through => :deal_participations
belongs_to:admin_user
#...更多逻辑在这里...
结束
然后,您需要三张表:一张用于交易
,一张用于 deal_participations
,和一个用于 standard_users
(加上管理员用户的东西)。
根据您的需要,您可能还想尝试使用单表继承(STI)使用户和管理用户从一个公共基类派生。您可以阅读更多关于STI 此处的信息。
我希望有所帮助!享受Rails入门!
I am building a daily deal app to better learn Ruby on Rails.
On M. Hartly tutorial, I understood the concept of models and their relationship (a microposts belongs_to a user, and a user has_many microposts).
However I am not sure how to put that into practice on my daily deal project.
I decided to have a Model for users with two type of users with different rights:
admin_user who can create/update/edit/delete the deals
standard_user who can see the deals and if they find one interesting participate in a deal
Both users go into the users database
I also decided to have a model for deals and go in the Deals database So have I understood well:
a admin_user has_many deals
deals belongs to a admin_user
standard_user has many deals (indeed he can participate in many deals)
and deals do NOT belong to standard_user so I can’t say anything here.
Have I understood things right? It is possible to define according to the type of right of a user different relationship to a model object(in this case: deals)
PS: I think I’ll separate users with cancan gem to give them different rights
The biggest issue with what you're describing is that a standard_user has_many :deals
, but deals don't belong to a standard_user; they probably are in use by many standard users at the same time. That means that a deal can't belong_to
a standard_user, because you can't have a standard_user_id
column in your Deals table (since you'd need arbitrarily many columns to deal with the arbitrarily many users who might participate).
In order to have a many-to-many relationship like this, you need a link table. Here's one way to achieve this in Rails:
class DealParticipation < ActiveRecord:Base
#This means the deal_participations table has a standard_user_id key
belongs_to :standard_user
#This means the deal_participations table has a deal_id key
belongs_to :deal
#... more logic goes here ...
end
Now your standard user looks like:
class StandardUser < ActiveRecord::Base
has_many :deal_participations
has_many :deals, :through => :deal_participations
# ... more logic goes here ...
end
And your deal class looks like:
class Deal < ActiveRecord::Base
has_many :deal_participations
has_many :standard_users, :through => :deal_participations
belongs_to :admin_user
#... more logic goes here ...
end
You'll then need three tables: one for deals
, one for deal_participations
, and one for standard_users
(plus the stuff for admin users).
Depending on your needs, you may also want to try using single-table inheritance (STI) to make Users and Admin Users derive from a common base class. You can read more about STI here.
I hope that helps! Enjoy getting started with Rails!
这篇关于Rails有许多和属于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!