1

https://github.com/soveran/ohm#modelsで説明されているように、モデル属性に ID を持つ配列を設定しようとしましたが、例外が発生しました。私のコードで何が間違っていますか?

モデル

class Event < Ohm::Model

attribute :title
set :attendees, :User

end

テスト

@fran = User.create(name: "Fran", email: 'fran@gmail.com')
@jose = User.create(name: "Jose", email: 'jose@hotmail.com')

event = Event.create(title: 'Party in Las Vegas', attendees: [@fran.id,@jose.id])

NoMethodError: undefined method `attendees=' for #<Event:0x000000020fb430>
    /home/ciro/.rvm/gems/ruby-2.1.3/gems/ohm-2.0.1/lib/ohm.rb:1470:in `block in update_attributes'
    /home/ciro/.rvm/gems/ruby-2.1.3/gems/ohm-2.0.1/lib/ohm.rb:1470:in `each'
    /home/ciro/.rvm/gems/ruby-2.1.3/gems/ohm-2.0.1/lib/ohm.rb:1470:in `update_attributes'
4

1 に答える 1

1

これを機能させるにはメソッドを使用する必要がありSet#add、そのメソッドは引数を 1 つしか取りません。

@fran = User.create(name: "Fran", email: 'fran@gmail.com')
@jose = User.create(name: "Jose", email: 'jose@hotmail.com')

event = Event.create(title: 'Party in Las Vegas')
event.attendees.add(@fran)
event.attendees.add(@jose)

これらが関連付けを意図している場合は、collections

于 2015-01-02T12:25:47.780 に答える