我有两种用户和彩票模型,它们通过桥表Lotteries_Users类名LotteriesUser成多对多关系。
以下是这些型号:
user.rb
class User < ApplicationRecord
has_many :lotteries_users, class_name: "LotteriesUser"
has_many :lotteries, through: :lotteries_users
end
lottery.rb
class Lottery < ApplicationRecord
has_many :lotteries_users, class_name: "LotteriesUser"
has_many :users, through: :lotteries_users
end
Lotteries_User模型:lotteries_user.rb
class LotteriesUser < ApplicationRecord
belongs_to :users
belongs_to :lotteries
end
我尝试在has_many关系之后使用类名,但仍然无法正常工作。我收到一个错误NoMethodError(对于用户:模块,未定义的方法'relation_delegate_class'):] >>在Rails控制台上Lottery.first.users
和在Rails控制台上<< NoMethodError(#的未定义方法`last')
User.first.lotteries我有两种用户和彩票模型,它们通过桥表Lotteries_Users类名LotteriesUser具有多对多的关系。这是这些模型:user.rb类User <...>class User < ApplicationRecord
has_many :lottery_users
has_many :lotteries, through: :lottery_users
end
# table should be named lottery_users
class LotteryUser < ApplicationRecord
belongs_to :users
belongs_to :lottery # should be singular
end
class Lottery < ApplicationRecord
has_many :lottery_users
has_many :users, through: :lottery_users
end
如果表名为Lotteries::User
,Rails会寻找lotteries_users
,因此,不正确的复数会引起很多问题。除非实际上存在更具描述性的内容,例如Ticket
或Entry
,否则联接模型的约定为SingularSingular。