On Wednesday, May 18, 2016 at 5:01:46 AM UTC-7, Tiago Cardoso wrote:
>
> Hi
>
> I guess you must have answered this before, I tried to look for the
> answer, but couldn't find anywhere. So, my apologies for the repeat.
>
> I have a one_to_many defined. Now, I want to add them all at once:
>
> class Car
> one_to_many :wheels
> end
>
> # this works
> 4.times.each { Car.first.add_wheel(Wheel.new)}
> # this would work in AR
> Car.first.wheels = 4.times.map { Wheel.new }
>
Sequel does not have association setters for one_to_many associations, it
only has add/remove methods:
car = Car.first
4.times do
car.add_wheel(Wheel.new)
end
>From what I see, the model doesn't provide the setter method in plain
> sequel. So I guess there must be an extension for this, although I couldn't
> find anything in the documentation.
>
> To be honest, I'd go for a workaround with the add_ methods, but the
> problem is, I'm using factory girl for my specs, and some of them involve
> settings some associations like:
>
> create(:car, wheels: [Wheel.new....
>
> So, I kind of need those, or a decent enough workaround.
>
You can look into the nested_attributes and/or association_pks plugins.
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.