Reputation: 23
I want to associate an array of objects with another object without specifying an id while I'm building the array. I'm pretty sure I've seen this done before, but can't find it.
class User < ActiveRecord::Base
has_many :companies
has_many :job_groups
end
class Job < ActiveRecord::Base
belongs_to :job_group
end
class JobGroup < ActiveRecord::Base
belongs_to :user
has_many :jobs
end
Company.rb
def self.user_links(user)
job_group = JobGroup.create(user_id: user.id)
user.companies.each do |c|
links = c.find_links
job_group << links
end
end
Links comes back as a collection of links and I want to associate it to a job group.
I get a NoMethodError: undefined method `<<' for
Upvotes: 0
Views: 30
Reputation: 2478
Not sure what you really want but clearly you can't push
or <<
an object to an object(in this case job_group
). You can only push object to array.
I assume you need another attribute of type Array in job_group
model so you can associate links
to it eg job_group.links << links
.
Hope it helps
Upvotes: 1