0

So, I'm fairly new to Rails and I'm stuck due to my model's complexity.

I have a Developer model, a Township model and a Project model and their contents are as follows:-

Developer.rb

Class Developer < ApplicationRecord
  has_many :townships,
  has_many :projects, through: :townships

  accepts_nested_attributes_for :township
end  

Township.rb

Class Township < ApplicationRecord
  belongs_to :developer
  has_many :projects

  accepts_nested_attributes_for :project
end  

Project.rb

Class Project < ApplicationRecord
  belongs_to :township

end  

I want to create projects such as follows:-

project = Developer.create(
          {
           name: 'Lodha',
           township_attributes: [
            {
             name: 'Palava', 
             project_attributes: [
              {
               name: 'Central Park'
              },
              {
               name: 'Golden Tomorrow'
              }
            ]}
          ]})  

Any ideas as to how can I accomplish this? I also need to understand the strong params whitelisting required in the DeveloperController.

8
  • are your associations correctly defined? Commented Mar 5, 2017 at 4:47
  • From what I understand, they are. Commented Mar 5, 2017 at 4:49
  • ok, but the relations are: 1) Developer is many-to-many with Project or 2) Developer has-many Township hast-many Project Commented Mar 5, 2017 at 5:05
  • Developer and township have a one to many and township and projects have a one to many mapping. So, developer and projects should have a one to many mapping as well through township. Commented Mar 5, 2017 at 5:08
  • is "a one to many" a has_many association? or a has_one AND a has_many association in the same model at same time? Commented Mar 5, 2017 at 5:16

1 Answer 1

3

I don't know of a way for you to create it in one line (plus it would be less readable) , but you can do this with rails using similar code to below:

def create
  developer = Developer.new(name: 'Loha')
  township = developer.townships.build({ name: 'Palava' })
    # for this part I don't know what your form looks like or what the 
    # params look like but the idea here is to loop through the project params like so
  params[:projects].each do |key, value|
    township.projects.build(name: value)
  end

  if developer.save
    redirect_to #or do something else
  end
end

Saving the developer will save all of the other things with the correct foreign keys assuming you have them set up correctly. Just pay attention to the format of your params to make sure you're looping through it correctly.

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

3 Comments

Thank you for your guidance. Yes, I understand your approach very well. I still do believe there must be a way to accomplish this in a go as the mapping is very simple and consistent. Regarding the model inheritance, Rails 5 now automatically generates an abstract class called ApplicationRecord which inherits ActiveRecord::Base so that no patching is to be done to customise or extend ActiveRecord's functionality.
Awesome, thanks for the correction, I didn't know that.
Sure, always a pleasure. 😁👍

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.