Ruby on Rails
Thursday, October 29, 2015
Can you try below solution:
private
def reject_contact(attributes)
attributes['phone'].blank? || attributes['website'].blank?
end
def reject_job(attributes)
attributes['company'].blank? || attributes['title'].blank?
end
On Thursday, October 29, 2015 at 12:29:09 PM UTC+5:30, Eduardo Campestrini wrote:
Hi!My reject_if filter is not working:2.2.3 :002 > params = { lead: {name: 'Eduardo', lastName: 'Campestrini', job_attributes: { company: 'RD' } } }=> {:lead=>{:name=>"Eduardo", :lastName=>"Campestrini", :job_attributes=>{:company=>"RD"}}} 2.2.3 :003 > lead = Lead.create(params[:lead])(0.1ms) begin transactionSQL (1.7ms) INSERT INTO "leads" ("name", "lastName", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "Eduardo"], ["lastName", "Campestrini"], ["created_at", "2015-10-29 03:35:49.845738"], ["updated_at", "2015-10-29 03:35:49.845738"]](1.2ms) commit transaction=> #<Lead id: 11, name: "Eduardo", lastName: "Campestrini", created_at: "2015-10-29 03:35:49", updated_at: "2015-10-29 03:35:49">2.2.3 :004 >class Lead < ActiveRecord::Basehas_one :contact, dependent: :destroyhas_one :job, dependent: :destroyaccepts_nested_attributes_for :contact, reject_if: :reject_contactaccepts_nested_attributes_for :job, reject_if: :reject_jobvalidates :name, presence: true, length: { maximum: 50 }validates :lastName, presence: true, length: { maximum: 100 }privatedef reject_contact(attributes)attributes['phone'].blank?attributes['website'].blank?enddef reject_job(attributes)attributes['company'].blank?attributes['title'].blank?endendclass Job < ActiveRecord::Basebelongs_to :leadvalidates :company, presence: true, length: { maximum: 20 }validates :title, presence: true, length: { maximum: 50 }endclass Contact < ActiveRecord::Basebelongs_to :leadvalidates :phone, presence: true, length: { maximum: 20 }validates :website, presence: true, length: { maximum: 50 }endThank,Eduardo
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d66dad11-9b1c-44d0-a472-c1d6b186091d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment