两个模型的特定关联包含相同的名称
Specific association of two model contains same name
我有两个型号 User
和 Speed
。用户有很多速度
用户包含 username, email, password, nameofwatch
速度包含nameofwatch, speed, userid
可以通过nameofwatch
关联两个模型。
如果 nameofwatch in user = nameofwatch in speed
出现current_user速度值
设置足够primary_key
and foreign-key
。在你的例子中,两者都是 nameofwatch
,因为你想在两个表中加入该列。
class User < ApplicationRecord
# use primary key of user as nameofwatch, and foreign key as nameofwatch in speed
has_many :speeds, foreign_key: 'nameofwatch', primary_key: 'nameofwatch'
end
class Speed < ApplicationRecord
belongs_to :user, foreign_key: 'nameofwatch', primary_key: 'nameofwatch'
end
我有两个型号 User
和 Speed
。用户有很多速度
用户包含 username, email, password, nameofwatch
速度包含nameofwatch, speed, userid
可以通过nameofwatch
关联两个模型。
如果 nameofwatch in user = nameofwatch in speed
出现current_user速度值
设置足够primary_key
and foreign-key
。在你的例子中,两者都是 nameofwatch
,因为你想在两个表中加入该列。
class User < ApplicationRecord
# use primary key of user as nameofwatch, and foreign key as nameofwatch in speed
has_many :speeds, foreign_key: 'nameofwatch', primary_key: 'nameofwatch'
end
class Speed < ApplicationRecord
belongs_to :user, foreign_key: 'nameofwatch', primary_key: 'nameofwatch'
end