如果我有两个表格users
和blocked_users
,我如何定义从blocked_users
到users
的关系?
例如,
class User < Sequel::Model
one_to_many :blocked_users, :key=>:source_id
one_to_many :blocked_by_users, :key=>:target_id, :class => BlockedUser
end
我会在这里放什么?:
# table => blocked_users, fields => "source_id", "target_id"
Class BlockedUser < Sequel:Model
#fail => belongs_to :blocking_user_account, :class => User, :primary_key => :source_id
#fail => belongs_to :blocked_user_account, :class => User, :primary_key => :target_id
end
我如何引用User
模型中的BlockedUser
模型?
我尝试了下面的修复,并对以下错误感到困惑;将BlockedUser类重组为如下:
class BlockedUser < Sequel::Model
many_to_one :blocking_user, :class=>:User, :primary_key => :source_id
many_to_one :blocked_user, :class=>:User, :primary_key => :target_id
end
我收到以下错误:
.9.3p0 :003 > BlockedUser.all.first.blocked_user
INFO - (0.000427s) SELECT * FROM `blocked_users`
NoMethodError: undefined method `blocked_user_id' for #<BlockedUser @values={:id=>1, :source_id=>1, :target_id=>2}>
但这确实执行了适当的查询:
class BlockedUser < Sequel::Model
many_to_one :source, :class=>:User
many_to_one :target, :class=>:User
end
BlockedUser.all.first.source
答案 0 :(得分:2)
您应该将类名指定为符号(并使用many_to_one而不是belongs_to):
class BlockedUser < Sequel:Model
many_to_one :blocking_user, :class=>:User, :primary_key => :source_id
many_to_one :blocked_user, :class=>:User, :primary_key => :target_id
end