I have 2 models, an artist and song model I have the validates_presence_of on each of the models (for checking title, artist name, and etc). They validate fine when you add items on their own controller (1 form for each model), but my program doesn''t validate when I combine both models in 1 form. -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
1. Maybe There''s no attribute in one of your model, you can use attr_accessor. 2. Have you tried like this ? ------------ Controller : ------------ def new_song #it uses Song Model @bad_song = Artist.new #it uses Artis Model @bad_artist = Artist.new end def record_it @bad_song = Song.new(params[:song]) @bad_artist = Artist.new(params[:artist]) if @bad_song.save else flash[:notice] = "Error.." end if @bad_artist.save else flash[:notice] = "Error.." end end ------------ View : ------------ <%#= comment your error_messages_for ''song'' %> <%#= comment your error_messages_for ''artist'' %> <%#= they will show you double tables error message %> <% form_tag :action => ''record_it'' do %> <%= text_field ''song'', ''title'' %> <%= text_field ''artist'', ''artist_name'' %> <%= submit_tag "save" %> <% end %> Have Nice Try and Good Luck. Would You mind to provide us your code so we can see your trouble location. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Reinhart Ariando WEB: Teapoci.Blogspot.com YM : Booking2Heaven MSN: reinhart.showbiz-EMRzualFZlQ@public.gmane.org ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
are artists and songs related? (I assume they are). If so, how? How
are you creating each model in your one form? You may want something
similar to the following:
class Artist < AR::Base
has_many :songs
validates_presence_of :name
end
class Song < AR::Base
belongs_to :artist
validates_presence_of :title
end
class SomeController < ActionController
def new
@artist = Artist.new
@song = @artist.songs.build
end
def create
@artist = Artist.new(params[:artist])
# append a new song to the list of songs for the artist
# you may want to loop through a collection of songs passed from the form
# if you want to provide the ability to add multiple songs at
once, or you could build
# this into the model
@song = @artist.songs.build(params[:songs])
if @artist.save
# both the artist and its related song objects are valid
else
# either artist was invalid or the song object was
end
end
end
and in your view:
views/artists/new.html.erb
<%= error_messages_for :artist, :song %>
Of course the above gets more complicated if you want to assign
multiple songs at a time, but this should at least get you started.
Mike
On Thu, Mar 6, 2008 at 3:53 PM, Feng Tien
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:>
> I have 2 models, an artist and song model
>
> I have the validates_presence_of on each of the models (for checking
> title, artist name, and etc). They validate fine when you add items on
> their own controller (1 form for each model),
>
> but my program doesn''t validate when I combine both models in 1
form.
> --
> Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Yow... Mike, YOurs is more reasonable than me. -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Feng Tien wrote:> I have 2 models, an artist and song model > > I have the validates_presence_of on each of the models (for checking > title, artist name, and etc). They validate fine when you add items on > their own controller (1 form for each model), > > but my program doesn''t validate when I combine both models in 1 form.Use validates_associated :song # in artist model validates_associated :artist # in song model http://ar.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000304 Stephan -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
stephen.celis-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Mar-07 00:06 UTC
Re: Validate 2 models in 1 form?
Stephan, Please note in the API:> class Book < ActiveRecord::Base > has_many :pages > validates_associated :pages > > Warning: If, after the above definition, you then wrote: > > class Page < ActiveRecord::Base > belongs_to :book > validates_associated :book > > ...this would specify a circular dependency and cause infinite recursion.On Mar 6, 5:17 pm, Stephan Wehner <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Feng Tien wrote: > > I have 2 models, an artist and song model > > > I have the validates_presence_of on each of the models (for checking > > title, artist name, and etc). They validate fine when you add items on > > their own controller (1 form for each model), > > > but my program doesn''t validate when I combine both models in 1 form. > > Use > > validates_associated :song # in artist model > > validates_associated :artist # in song model > > http://ar.rubyonrails.com/classes/ActiveRecord/Validations/ClassMetho... > > Stephan > > -- > Posted viahttp://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
stephen.celis-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Stephan, > > Please note in the API: > >> ...this would specify a circular dependency and cause infinite recursion. > On Mar 6, 5:17 pm, Stephan Wehner <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>I see, then can add a field like this class Artist < ActiveRecord::Base :attr_accessor skip_validate_song validates_associated :song, :if => Proc.new { |record| !record.skip_validate_song } end (and / or equivalent to Song class) Plus set that in the controller as the case may be. Stephan -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---