Here is my model looks like:-
Model: Movie
has_many :songs
Model: Song
belongs_to :movie
I have a songs/new form which contains song name and movie name input
field.
If movie name does not exist it should create a new one else it should
use
already existing one. So this I will come to know only after user enter
in
movie name field.
Can I achieve this using nested attributes of rails 3?
Note: I can able to implement the same in has_many association but not
in
this case.
You should look at before_create in ActiveRecord. I think it would be
better if you put on in your Song Model that calls private function to
check whether the passed in Movie name exists or not. If it doesn’t,
create it and if it does exist do nothing.
For first part of my problem, I am able to figure out the nested
attributes
with belongs_to.
For second part, I am looking for Rails way solution to my problem.
Using nested attribute. I can able to create Movie if it does not exist.
And
able to associate it with song model (movie_id populated correctly)
work
really good by rails way. I want this behavior.
But when movie already exist. I want to associate that movie id with
the
new song to be created using accepts_nested_attributes_for.
*
*
Thanks!
Butu
Facing one problem when I am using accepts_nested_attributes_for and
has_many. This is working in rails way and able to insert record into
songs,
people and people_song table correctly. But again I want to use similar
logic find and initilize. But in that case I may need to insert into
people_song table manually. Any thoughts on this?
Thanks!
Butu
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.