Custom accessor for array element

Posted by memph1s on Stack Overflow See other posts from Stack Overflow or by memph1s
Published on 2010-04-04T11:47:56Z Indexed on 2010/04/04 11:53 UTC
Read the original article Hit count: 230

Filed under:
|

I'm trying to create an accessor for one element from array with specific flag set to true:

class EntranceObject < ActiveRecord::Base
  has_many :subscribers

  def customer
      self.subscribers.find(:first, :conditions => {:is_customer => true})
  end

  def customer=(customer_params)
    self.subscribers << Subscriber.new(:name => customer_params[:name],
                                       :apartment => customer_params[:apartment],
                                       :phone_number => customer_params[:phone_number],
                                       :is_customer => true)
  end
end

class Subscriber < ActiveRecord::Base
  belongs_to :entrance_object

  validates_presence_of :name, :apartment         
end

How do i need to validate this accessor in order to hightlight missing fields in a view?

P.S. I'm newbie in RoR, maybe there is another approach to such work with one element from a collection? Thanks.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about rails