ruby on rails - Combining virtual attributes to single class attributes in super class (STI) -


i'm trying combine start_date, start hour, , start_minute virtual attributes event form, in order create start_datetime attribute (which stored in database).

i have (via sti) several subclasses of event; let's call them trainingsession , worksession , personaltime.

classes structured such:

class event < activerecord::base   ... end  class trainingsession < event   ... end  class worksession < event   ... end  class personaltime < event   ... end 

the relevant parts of event.rb:

class event < activerecord::base   attr_accessor :start_date, :start_hour, :start_minute    validates :start_datetime, :presence => true    before_validation :merge_attributes_for_datetime_string    def merge_attributes_for_datetime_string     start_datetime_string = "#{ start_date } #{ start_hour }:#{ start_minute }:00"   end    def start_datetime=(start_datetime_string)     self.start_datetime = start_datetime_string   end    def start_date     start_datetime.strftime("%d") if start_datetime?   end    def start_hour     start_datetime.strftime("%h") if start_datetime?   end    def start_minute     start_datetime.strftime("%m") if start_datetime?   end end 

... , of events_controller.rb:

  def create     @event = event.new(event_params)     if @event.save       redirect_to :root, :flash => { :success => "event added." }     else       redirect_to :back, :flash => { :notice => "there error creating event." }     end   end    private    def event_params     params.require(:event).permit(       :type,       :start_datetime,       :start_date,       :start_hour,       :start_minute,       ...     )   end    def training_session_params     params.require(:training_session).permit(       ...     )   end    def work_session_params     params.require(:work_session).permit(       ...     )   end    def personal_time_params     params.require(:personal_time).permit(       ...     )   end 

i've verified in server logs correct params being sent form:

parameters: {"utf8"=>"✓", "authenticity_token"=>"<token here>=", "event"=>{"start_date" => "2013-08-23", "start_hour"=>"15", "start_minute"=>"00", "type"=>"personaltime"}, "commit"=>"add personal time"} 

yet every time try create event (of type), notice there error creating event. (as per create method). if comment out validates :start_datetime, event created, start_datetime of nil.

this has mean start_datetime string isn't being merged virtual attributes, can't figure out why.

what missing here? there better way set start_datetime?

based on you've posted, don't see calling start_datetime method.

instead of defining new method, merging in start_datetime method follows:

before_validation :merge_attributes_for_datetime_string  def merge_attributes_for_datetime_string   self.start_datetime = "#{ start_date } #{ start_hour }:#{ start_minute }:00" end 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -