You don't need to use nested attributes for that. You can achieve the
same result with a pretty simple virtual attribute:

class Song < ActiveRecord::Base
  belongs_to :movie

  def movie_name=(name)
    @movie_name = name
    self.movie = Movie.find_or_initialize_by_name(name)
  end

  def movie_name
    @movie_name
  end
end

And then on your form:

= form_for @song do |form|
  = form.text_field :movie_name

On Feb 23, 3:13 pm, Butu <but...@gmail.com> wrote:
> Hello Jim,
>
> Thanks for your reply.
>
>  I can able to do accepts_nested_attributes with belongs_to
>
> @song.build_movie
>
> use this either in action or before the form and use fields_for. This will
> create the new movie record and insert movie_id in song model as well.
>
> On Wed, Feb 23, 2011 at 2:12 PM, Jim Ruther Nill <jvn...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
> > On Wed, Feb 23, 2011 at 2:54 PM, Butu <but...@gmail.com> wrote:
>
> >> Thanks Bryan for your reply.
>
> >> 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.*
> >> *
> >> *
>
> > you can't. accepts_nested_attributes_for doesn't work on belongs_to
> > attribute.
>
> >> **
> >> Thanks!
> >> Butu
>
> >> On Tue, Feb 22, 2011 at 10:09 PM, Bryan Crossland 
> >> <bacrossl...@gmail.com>wrote:
>
> >>> Butu,
>
> >>> 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.
>
> >>> Thanks,
>
> >>> B.
>
> >>> On Mon, Feb 21, 2011 at 9:42 PM, Butu <but...@gmail.com> wrote:
> >>> > 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.
> >>> > Thanks!
> >>> > Butu
>
> >>> > --
> >>> > You received this message because you are subscribed to the Google
> >>> Groups
> >>> > "Ruby on Rails: Talk" group.
> >>> > To post to this group, send email to rubyonrails-talk@googlegroups.com
> >>> .
> >>> > To unsubscribe from this group, send email to
> >>> > rubyonrails-talk+unsubscr...@googlegroups.com.
> >>> > For more options, visit this group at
> >>> >http://groups.google.com/group/rubyonrails-talk?hl=en.
>
> >>  --
> >> You received this message because you are subscribed to the Google Groups
> >> "Ruby on Rails: Talk" group.
> >> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> rubyonrails-talk+unsubscr...@googlegroups.com.
> >> For more options, visit this group at
> >>http://groups.google.com/group/rubyonrails-talk?hl=en.
>
> > --
> > -------------------------------------------------------------
> > visit my blog athttp://jimlabs.heroku.com
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Ruby on Rails: Talk" group.
> > To post to this group, send email to rubyonrails-talk@googlegroups.com.
> > To unsubscribe from this group, send email to
> > rubyonrails-talk+unsubscr...@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/rubyonrails-talk?hl=en.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to