1

I have tried to set an array with ids in a model attribute like is explained within https://github.com/soveran/ohm#models but I get an exception. What is wrong in my code?

Model

class Event < Ohm::Model

attribute :title
set :attendees, :User

end

Test

@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'
Fran b
  • 3,016
  • 6
  • 38
  • 65
  • you have a typo - once you write `attendes` and once `attendees` – Uri Agassi Jan 02 '15 at 11:49
  • Sorry it was a mistake in the "copy - paste". Now is fixed in the post. In the model I have "attendees" and in the test too. I have check it. Any idea? – Fran b Jan 02 '15 at 11:58

1 Answers1

1

You need to use the Set#add method for this to work and that method only takes 1 argument:

@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)

If these are meant to be associations then I'd look at collections

Anthony
  • 15,435
  • 4
  • 39
  • 69