Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8653215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:29:22+00:00 2026-06-12T14:29:22+00:00

So I have a nested resource with resources :albums do resources :elements end I

  • 0

So I have a nested resource with

resources :albums do 
  resources :elements
end

I can update, delete and view those elements.
What I can not really do is to create a new element.
So it is actually created in the database but not in the mapping table.
Models:

class Album:

has_many :elements, :through => :albums_elements      
has_many :albums_elements

class Element:

has_many :albums_elements
has_one  :album, :through => :albums_elements

class AlbumsElement:

belongs_to :album
belongs_to :element

In the element Controller I have:

def create
@element = Element.new(params[:element])
@album   = Album.find(params[:album_id])
respond_to do |format|
  if @element.save
    format.html { redirect_to album_elements_path(@album), :notice => 'Element was successfully created.' }
    format.json { render :json => @element, :status => :created, :location => @element }
  else
    format.html { render :action => "new" }
    format.json { render :json => @element.errors, :status => :unprocessable_entity }
  end
end
end

So as I said, when I press the create button in the form, the element is being created correctly in the table “elements”, but not inserted into “albums_elements”.
I saw a similar post here, where the author was told to fix his dependencies.
But I don’t see an error in mines?
How do I tell rails to insert into both tables?
elements AND albums_elements?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:29:23+00:00Added an answer on June 12, 2026 at 2:29 pm

    Would you please check by relating your @element and @album in elements_controller class? Like you already have

    @element = Element.new(params[:element])
    @album   = Album.find(params[:album_id])
    

    Just add

    @element.album = @album
    

    Now @element.save should insert a entry in your relationship table also.

    Let me know if it works. I’ve just answered after a glance to the problem.

    Clarifications:

    I will try to make a sample app for you. But before that i need to know some more.

    • Seems the relationship between Album and Element is one-to-many, then why you are using has_many :through? It could be simply Album: has_many :products and Product: belongs_to :album.

    • If you must use a relationship table, does it have any other attributes?

    • Is it OK if I provide any solution of one-to-many?

    I will try to provide you the best rails way to save these entries asap.
    Thnx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a nested resource like so: resources :users resources :posts end and
I am trying to create nested resources where the sub-resource doesn't have an existence
I have the following rails 3 nested resource: resources :books do resources :authors end
I have a nested resource, setup like this: resources :chickens do resources :eggs end
I have a nested resource that looks like this: resources :events resources :attendances post
I have 2 resources, one being a nested resource of another: parent_resource and child_resource
Okay, I have the following create action #posts_controller, nested resource under discussions def create
Yeah the problem is, that I created a nested resource like this: resources :albums
I have the following nested resources in my routes.rb file. The inner resource specifies
I have a nested resources like this resources :profiles do resources :albums do resources

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.