我是Rails的新手,我正在尝试建立多态HABTM关系.问题是我有三个我想要关联的模型.
第一个是事件模型,然后是两种与会者:用户和联系人.
我想要做的是能够作为参与者与用户和联系人联系.那么,我现在在我的代码中拥有的是:
事件模型
has_and_belongs_to_many :attendees, :polymorphic => true
Run Code Online (Sandbox Code Playgroud)
用户模型
has_and_belongs_to_many :events, :as => :attendees
Run Code Online (Sandbox Code Playgroud)
联系型号
has_and_belongs_to_may :events, :as => :attendees
Run Code Online (Sandbox Code Playgroud)
polymorphism activerecord relationship has-many-polymorphs ruby-on-rails-3
我得到了一些Manager和SoccerTeam模特.一名经理"拥有"许多足球队; 经理也可以评论足球队,也可以评论其他经理:
manager.rb
# Soccer teams the manager owns
has_many :soccer_teams, :dependent => :restrict
# Comments the manager has made on soccer teams or other managers
has_many :reviews, :class_name => "Comment", :foreign_key => :author_id, :dependent => :destroy
# Comments the manager has received by other managers
has_many :comments, :as => :commentable, :dependent => :destroy
# Soccer teams that have received a comment by the manager
has_many :observed_teams, :through => :comments, :source => :commentable, :source_type => "SoccerTeam" …Run Code Online (Sandbox Code Playgroud) model-view-controller ruby-on-rails has-many has-many-polymorphs has-many-through
我有以下型号.用户有UserActions,一个可能的UserAction可以是ContactAction(UserAction是一个多态).还有LoginAction等其他动作.所以
class User < AR::Base has_many :contact_requests, :class_name => "ContactAction" has_many :user_actions has_many_polymorphs :user_actionables, :from => [:contact_actions, ...], :through => :user_actions end class UserAction < AR::Base belongs_to :user belongs_to :user_actionable, :polymorphic => true end class ContactAction < AR::Base belongs_to :user named_scope :pending, ... named_scope :active, ... end
这个想法是一个ContactAction加入两个用户(在应用程序中有其他后果),并始终有一个接收端和一个发送端.同时,ContactAction可以具有不同的状态,例如过期,待定等.
我可以说@user.contact_actions.pending或@user.contact_requests.expired列出用户发送或接收的所有待处理/过期请求.这很好用.
我现在想要的是一种加入两种类型的ContactAction的方法.即@user.contact_actions_or_requests.我尝试了以下方法:
class User def contact_actions_or_requests self.contact_actions + self.contact_requests end # or has_many :contact_actions_or_requests, :finder_sql => ..., :counter_sql => ... end
但是所有这些都存在这样的问题:在关联之上不可能使用额外的finder或named_scopes,例如@user.contact_actions_or_requests.find(...)或 …
这个真的让我失望!:(
我正在尝试为我的用户模型创建一个嵌套模型表单,其中包含一个复选框列表,其中可以检查或取消选中多个商店以通过模型人员配置来管理商店.
class Staffing < ActiveRecord::Base
# Associations
belongs_to :user
belongs_to :staffable, :polymorphic => true
belongs_to :store, :class_name => "Store",
:foreign_key => "staffable_id"
end
class User < ActiveRecord::Base
# Includes
acts_as_authentic
# Associations
has_many :staffings, :dependent => :destroy
has_many :stores, :through => :staffings
# Nested Attributes
accepts_nested_attributes_for :staffings, :allow_destroy => true
end
class Store < ActiveRecord::Base
# Associations
has_many :staffings, :as => :staffable, :dependent => :destroy
has_many :users, :through => :staffings
end
# Users Controller
def create
@user = User.new(params[:user]) …Run Code Online (Sandbox Code Playgroud) forms checkbox ruby-on-rails has-many-polymorphs has-many-through
polymorphism ×2
activerecord ×1
associations ×1
checkbox ×1
forms ×1
has-many ×1
relationship ×1