更新:
我已将其放入我的 person
中类
has_many :things, :dependent => :destroy do
def [](kind)
where("kind = ?", kind.to_s)
end
end
但是当我调用<Person Instance>.things[:table]
时,我收到此错误:
undefined method `where' for #<Class:0x111dc3ba8>
原始问题:
我有一个person
,谁has_many
things
。
我希望能够做这样的事情:
<Person Instance>.things[:table]
其定义类似于
def things[](arg)
self.things.find(:first, :conditions => ["kind = ?", arg.to_s])
end
目前,该方法给了我这个错误:
syntax error, unexpected '[', expecting '\n' or ';'
那么,我如何正确定义事物[]?
最佳答案
您正在寻找的内容称为 Rails 中的关联扩展。 Read about it here
您的实现可能类似于:
has_many :things do
def [](kind)
where(:kind => kind)
end
end
关于ruby-on-rails - 我如何定义关联的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12060065/