I have Admin class & User class and a polymorphic Profile class

class Admin < ActiveRecord::Base
include ActiveModel::Validations
has_one :profile, :as => :profitable
accepts_nested_attributes_for :profile
validates_associated :profile

class User < ActiveRecord::Base
include ActiveModel::Validations
has_one :profile, :as => :profitable

class Profile < ActiveRecord::Base
belongs_to :profilable, :polymorphic => true
validates :title, :first_name, :last_name , :presence => true, :if
=>Proc.new { |p| p[:profilable_type] == "Admin" }

I am trying to validates a new Admin form with nested attributes
but I don't need the profile validation for the User class,
I wrote a validation in the Profile class with a Proc block to test
the profilable_type, but it's not working ....
is there another way to try ?



--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Peter Vandenabeele at Jan 19, 2012 at 5:13 pm

    On Thu, Jan 19, 2012 at 3:24 PM, Erwin wrote:

    I have Admin class & User class and a polymorphic Profile class

    class Admin < ActiveRecord::Base
    include ActiveModel::Validations
    has_one :profile, :as => :profitable
    accepts_nested_attributes_for :profile
    validates_associated :profile

    class User < ActiveRecord::Base
    include ActiveModel::Validations
    has_one :profile, :as => :profitable

    class Profile < ActiveRecord::Base
    belongs_to :profilable, :polymorphic => true
    validates :title, :first_name, :last_name , :presence => true, :if
    =>Proc.new { |p| p[:profilable_type] == "Admin" }

    I am trying to validates a new Admin form with nested attributes
    but I don't need the profile validation for the User class,
    I wrote a validation in the Profile class with a Proc block to test
    the profilable_type, but it's not working ....
    is there another way to try ?

    Maybe(??) there is a mix-up between "profitable" and "profilable",
    or is that intentional?

    HTH,

    Peter

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Erwin at Jan 19, 2012 at 5:52 pm
    Thanks Peter .. just a typing error in my post , actually it's well
    written in the models : has_one :profile, :as => :profitable ( even
    if profitable would make $$)

    the params sent from the form submit are OK

    {.. "admin"=>{"area_id"=>"1", "canAdministrate"=>"0",
    "canManageProject"=>"0", "canShootVideo"=>"0", "canEditVideo"=>"0",
    "email"=>"", "password"=>"", "password_confirmation"=>"",
    "profile"=>{"first_name"=>"", "middle_name"=>"", "last_name"=>"",
    "phone"=>"", "mobile"=>"", "address"=>""}},
    "commit"=>"Enregistrer", "action"=>"create", "controller"=>"backoffice/
    admins"}

    you can see params[:admin] and params[:admin][:profile]
    in the edit action, if I execute a
    @admin = Admin.new(params[:admin])
    @admin.valid? => false
    @admin.errors = #<ActiveModel::Errors:0x007fde974fc928 @base=#<Admin
    id: ....@messages={:email=>["doit être rempli(e)"], :password=>["doit
    être rempli(e)"]}
    I get only the email and password errors even with a validation in the
    Profile model
    validates :title, :first_name, :last_name , :presence => true



    On Jan 19, 6:13 pm, Peter Vandenabeele wrote:
    On Thu, Jan 19, 2012 at 3:24 PM, Erwin wrote:
    I have  Admin class & User class and a polymorphic Profile class
    class Admin < ActiveRecord::Base
    include ActiveModel::Validations
    has_one :profile, :as => :profitable
    accepts_nested_attributes_for :profile
    validates_associated :profile
    class User < ActiveRecord::Base
    include ActiveModel::Validations
    has_one :profile, :as => :profitable
    class Profile < ActiveRecord::Base
    belongs_to :profilable, :polymorphic => true
    validates :title, :first_name, :last_name , :presence => true, :if
    =>Proc.new { |p| p[:profilable_type] == "Admin" }
    I am trying to validates a new Admin form with nested attributes
    but I  don't need the profile validation for the User class,
    I wrote a validation in the Profile class  with a Proc block to test
    the profilable_type, but it's not working ....
    is there another way to try ?
    Maybe(??) there is a mix-up between "profitable" and "profilable",
    or is that intentional?

    HTH,

    Peter
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJan 19, '12 at 2:25p
activeJan 19, '12 at 5:52p
posts3
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Erwin: 2 posts Peter Vandenabeele: 1 post

People

Translate

site design / logo © 2021 Grokbase