Ruby on rails Rails模型关联,有多个:通过,并且有一个与同一模型关联
我有两个模型:用户和状态。州模型为美国50个州中的每个州都有记录 我希望每个用户都有两个属性:一个“home”状态和多个“visit”状态 我知道我必须建立一些模型关联来实现这一点,但不确定最好的方法是什么 这是我到目前为止所知道的,但我知道同一个模型的多个和一个关联一定有问题Ruby on rails Rails模型关联,有多个:通过,并且有一个与同一模型关联,ruby-on-rails,activerecord,associations,Ruby On Rails,Activerecord,Associations,我有两个模型:用户和状态。州模型为美国50个州中的每个州都有记录 我希望每个用户都有两个属性:一个“home”状态和多个“visit”状态 我知道我必须建立一些模型关联来实现这一点,但不确定最好的方法是什么 这是我到目前为止所知道的,但我知道同一个模型的多个和一个关联一定有问题 #user.rb class User < ActiveRecord::Base has_many :visits has_many :states, :through => :visits
#user.rb
class User < ActiveRecord::Base
has_many :visits
has_many :states, :through => :visits
has_one :state
end
#visit.rb
class Visit < ActiveRecord::Base
belongs_to :user
belongs_to :state
end
#state.rb
class State < ActiveRecord::Base
has many :visits
has many :users, :through => :visits
belongs_to :user
end
#user.rb
类用户:访问
有一个:国家
结束
#visit.rb
课堂访问:访问
属于:用户
结束
有什么建议吗?在这种情况下,您不能在一个模型上有多个和一个关系。一种解决办法是: 创建状态的静态模型,它们不需要是数据库模型,它们可以是状态模型上的静态变量:
US\u states={'1'=>'AK','2'=>'AL',etc}
或者您可以使用fixture将状态表加载到数据库中(更复杂,因为需要使用rake任务或db:seed任务将装置加载到db中,但是很好,因为可以使用activerecord来管理模型)
然后,您可以在用户模型上提供一个home\u state\u id来定义home\u state,访问只是用户id和state\u id之间的连接。在这种情况下,单个模型上不能有多个和一个关系。一种解决方案是: 创建状态的静态模型,它们不需要是数据库模型,它们可以是状态模型上的静态变量:
US\u states={'1'=>'AK','2'=>'AL',etc}
或者您可以使用fixture将状态表加载到数据库中(更复杂,因为需要使用rake任务或db:seed任务将装置加载到db中,但是很好,因为可以使用activerecord来管理模型)
然后,您可以在用户模型上提供一个home_state_id,该用户模型定义了home_状态,访问只是用户_id和state_id之间的连接
我希望每个用户都有两个属性:一个“home”状态和多个“visit”状态
在您的型号中,一个状态只能是一个用户的家(属于
)
正确的语义应该是
class User < AR::Base
belongs_to :home_state, :class_name => "State", :foreign_key => "home_state_id", :inverse_of => :users_living
has_and_belongs_to_many :visited_states, :through => :visits
# ...
end
class State < AR::Base
has_many :users_living, :class_name => "User", :inverse_of => :home_state
# ...
end
class用户“state”、:foreign\u key=>“home\u state\u id”、:inverse\u of=>:users\u living
你和你是否属于许多:访问过的州,:通过=>:访问
# ...
结束
类状态“用户”,:相反的=>:主状态
# ...
结束
我希望每个用户都有两个属性:一个“home”状态和多个“visit”状态
在您的型号中,一个状态只能是一个用户的家(属于
)
正确的语义应该是
class User < AR::Base
belongs_to :home_state, :class_name => "State", :foreign_key => "home_state_id", :inverse_of => :users_living
has_and_belongs_to_many :visited_states, :through => :visits
# ...
end
class State < AR::Base
has_many :users_living, :class_name => "User", :inverse_of => :home_state
# ...
end
class用户“state”、:foreign\u key=>“home\u state\u id”、:inverse\u of=>:users\u living
你和你是否属于许多:访问过的州,:通过=>:访问
# ...
结束
类状态“用户”,:相反的=>:主状态
# ...
结束
在我看来,您已经拥有的几乎是正确的,只是您将在用户身上存储本机状态外键,如下所示:
# user.rb
class User < ActiveRecord::Base
belongs_to :state
has_many :visits
has_many :states, through: visits
end
# visit.rb
class Visit < ActiveRecord::Base
belongs_to :user
belongs_to :state
end
# state.rb
class State < ActiveRecord::Base
has_many :visits
has_many :users, through: :visits
end
u = User.first
u.state
u = User.first
u.states
以及访问过的国家,如:
# user.rb
class User < ActiveRecord::Base
belongs_to :state
has_many :visits
has_many :states, through: visits
end
# visit.rb
class Visit < ActiveRecord::Base
belongs_to :user
belongs_to :state
end
# state.rb
class State < ActiveRecord::Base
has_many :visits
has_many :users, through: :visits
end
u = User.first
u.state
u = User.first
u.states
为了编程清晰,您可以重命名关系:
# user.rb
class User < ActiveRecord::Base
belongs_to :home_state, class_name: "State"
has_many :visits
has_many :visited_states, class_name: "State", through: visits
end
# state.rb
class State < ActiveRecord::Base
has_many :residents, class_name: "User"
has_many :visits
has_many :visitors, class_name: "User", through: :visits
end
我希望您可能希望存储有关访问的其他信息,因此保留访问
模型的HMT join表将允许您执行此操作,而不是使用HABTM关系。然后您可以为访问添加属性:
# xxxxxxxxxxxxxxxx_create_visits.rb
class CreateVisits < ActiveRecord::Migration
def change
create_table :visits do |t|
t.text :agenda
t.datetime commenced_at
t.datetime concluded_at
t.references :state
t.references :user
end
end
end
#xxxxxxxxxxxxxx_创建_访问.rb
类CreateVisits
在我看来,您已经拥有的几乎是正确的,只是您将在用户身上存储本机状态外键,如下所示:
# user.rb
class User < ActiveRecord::Base
belongs_to :state
has_many :visits
has_many :states, through: visits
end
# visit.rb
class Visit < ActiveRecord::Base
belongs_to :user
belongs_to :state
end
# state.rb
class State < ActiveRecord::Base
has_many :visits
has_many :users, through: :visits
end
u = User.first
u.state
u = User.first
u.states
以及访问过的国家,如:
# user.rb
class User < ActiveRecord::Base
belongs_to :state
has_many :visits
has_many :states, through: visits
end
# visit.rb
class Visit < ActiveRecord::Base
belongs_to :user
belongs_to :state
end
# state.rb
class State < ActiveRecord::Base
has_many :visits
has_many :users, through: :visits
end
u = User.first
u.state
u = User.first
u.states
为了编程清晰,您可以重命名关系:
# user.rb
class User < ActiveRecord::Base
belongs_to :home_state, class_name: "State"
has_many :visits
has_many :visited_states, class_name: "State", through: visits
end
# state.rb
class State < ActiveRecord::Base
has_many :residents, class_name: "User"
has_many :visits
has_many :visitors, class_name: "User", through: :visits
end
我希望您可能希望存储有关访问的其他信息,因此保留访问
模型的HMT join表将允许您执行此操作,而不是使用HABTM关系。然后您可以为访问添加属性:
# xxxxxxxxxxxxxxxx_create_visits.rb
class CreateVisits < ActiveRecord::Migration
def change
create_table :visits do |t|
t.text :agenda
t.datetime commenced_at
t.datetime concluded_at
t.references :state
t.references :user
end
end
end
#xxxxxxxxxxxxxx_创建_访问.rb
类CreateVisits
PS.你启发我将这篇文章写到一篇我很久以来一直想写的帖子中:谢谢!因此,本质上,我可以删除has\u one关系,而在用户模型中有一个名为:home\u state的表列,它引用state静态模型中的:state\u id?我应该为访问保留has\u many:through关系吗哦,太酷了,刚刚看到了你新帖子的链接。我对Rails完全是个新手,所以我会尝试将你在帖子中写的内容应用到我的家乡州和其他州来解决这个问题!如果你采用静态列表的方式,为了澄清这一点,你会创建一个模块,就像这样:它可能位于/lib/us_state.rb中,然后你会将该模块包含在任何模型中这需要了解状态。类Foohas\u many/has\u one
关联,只要它们的名称不同并且代表不同的概念。例如:用户