0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.