1

如果我有两个表,users并且blocked_users,我将如何定义从blocked_usersto的关系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
4

1 回答 1

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
于 2012-06-28T17:52:56.490 回答