Generic-user-small Stuart Fraser 3 posts

I have been following some other tutorials and examples where the id is not used on an association table. However in the example code here the association table seems to rely on the row id.

def existing_showing_attributes=(showing_attributes)
... attributes = showing_attributes[showing.id.to_s]
...
end

My association table doesn’t have a row_id, can I still implement this? I am not quite sure how to proceed.

I am showing all questions for a page /pages/1/
My pages_questions table is [page_id:int, question_id:int, order:int]

{“commit”=>”Submit”, “_method”=>”put”, “authenticity_token”=>”211f1b03d550457151247a05219dac12324083a9”, “id”=>”1”, “page”=>{“existing_pages_question_attributes”=>[{“question_id”=>”1”, “page_id”=>”1”, “order”=>”1”}, {“question_id”=>”2”, “page_id”=>”1”, “order”=>”1”}]}}

Can I use your example without a row id?
Thanks

 
Generic-user-small Stuart Fraser 3 posts

I must point out that I can add the first entry without this row id. It just has issues when updating because I need to update fields with a “WHERE page_id = ‘1’ AND question_id =’4’”

UPDATE

I have put a row id in to make accessing each row easier. Now I have another issue. If I use a text_field for my version of the ‘showing’ form I don’t get an id in the name="page[existing_pages_question_attributes][][order]" and submitting this gives me a 500 internal server error and says something about a clash between a hash and an array. If I manually add one then the select ends up with an extra [1][][select].

Once I have fixed adding this index depending on what type I have then everything works. Checked it all out with firebug.

Regards

 
Generic-user-small Stuart Fraser 3 posts

Aha.

As I am new I didn’t realise that I needed to remove the :index => option from the text_field rendering

I still don’t really understand the following snippet though.

:index => (pages_question.new_record? ? '' : nil)

Anyway, I have it all working now.

3 posts, 1 voice