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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
user1697061
  • 255
  • 3
  • 14

1 Answers1

0

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

Samiron
  • 5,169
  • 2
  • 28
  • 55
  • Oh I did not see the reply, sorry. Unfortunately not working. The log says: NoMethodError (undefined method `each' for #): app/controllers/elements_controller.rb:46:in `create' It looks like this: @element = Element.new(params[:element]) @album = Album.find(params[:album_id]) @element.album = @album Now, not even element was created – user1697061 Oct 10 '12 at 12:49
  • I also had to change the element model to: has_many :album, :through => :albums_elements | instead of | has_one :album, :through => :albums_elements - but the problem is still there. – user1697061 Oct 10 '12 at 13:24
  • I solved it by using an own insert on the relationship-table: new_element = Element.last element_id = new_element.element_id AlbumsElement.create!(:album_id => params[:album_id], :element_id => element_id) – user1697061 Oct 10 '12 at 14:41
  • Great !! But this is so manual. I am sure that there is much more easier solution for this. I will try to make a sample of that shortly. But before that, would you please clarify my questions added to my answer? – Samiron Oct 10 '12 at 15:03
  • Very nice thanks. So the relationship table must be used ... in fact it is possible, that 1 element also belongs to many albums - so it is many to many :through the relationship table. – user1697061 Oct 19 '12 at 08:07