Building Assocation with multiple belong_tos

7 views
Skip to first unread message

Jarod Watkins

unread,
Dec 12, 2012, 10:18:01 AM12/12/12
to rubyonra...@googlegroups.com
Hello,
I have two models that look like the following:

ModelA:
  attr_accessible :date, ...
  has_many :modelb
  accepts_nested_attributes_for :modelb
end

ModelB:
  attr_accessible :date, ...
  belongs_to :modela, :modelc, :modeld
end

And then in ModelAs controller:

@modela = Modela.new

3.times do |i|
  @modela.modelb.build(:date => Time.now)
end

When ModelB has 3 belongs_to and I attempt to build the association with just ModelA I get the following error:

ArgumentError wrong number of arguments (3 for 2)

When ModelB just belongs_to modela it works. Do I need to somehow setup a fake association for the other belong_tos? Those are to be created in the UI by the user.

Thanks,
Jarod

Colin Law

unread,
Dec 12, 2012, 10:28:21 AM12/12/12
to rubyonra...@googlegroups.com
On 12 December 2012 15:18, Jarod Watkins <jwat...@jarodw.com> wrote:
> Hello,
> I have two models that look like the following:
>
> ModelA:
> attr_accessible :date, ...
> has_many :modelb

That should be modelbs, plural.

> accepts_nested_attributes_for :modelb
> end
>
> ModelB:
> attr_accessible :date, ...
> belongs_to :modela, :modelc, :modeld

I think you have to specify those on separate lines.

Colin

> end
>
> And then in ModelAs controller:
>
> @modela = Modela.new
>
> 3.times do |i|
> @modela.modelb.build(:date => Time.now)
> end
>
> When ModelB has 3 belongs_to and I attempt to build the association with
> just ModelA I get the following error:
>
> ArgumentError wrong number of arguments (3 for 2)
>
> When ModelB just belongs_to modela it works. Do I need to somehow setup a
> fake association for the other belong_tos? Those are to be created in the UI
> by the user.
>
> Thanks,
> Jarod
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-ta...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rubyonrails-talk/-/Gv5tkWCmhlQJ.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Jarod Watkins

unread,
Dec 12, 2012, 9:11:25 PM12/12/12
to rubyonra...@googlegroups.com
Ah thanks Colin, that was it!
Reply all
Reply to author
Forward
0 new messages