Question

So I have a rails model that stores hours of operation as a postgres tstzrange, to accomplish this with rails I use the virtual attributes opens_at and closes_at. Currently I have database columns for these attributes that don't save anything, but I have to have them because rails will complain about not knowing the class of the attribute receiving the multiple parameter assignment unless it maps to a data type.

There are a few extra moving pieces here that don't really matter so they weren't included.

I guess what I'm asking is: Is there a way to remove the DB columns closes_at and opens_at and still be able to submit a datetime over a web for and have rails serialize it? Can I somehow use something like attr_accessible to point to a data type?

class OperationDay

  def closes_at=(close)
    close = to_2012(close)
    Rails.logger.debug "OD-#{self.id} set closes_at to #{close}"
    @closes_at = close
    check_wrap
  end

  def opens_at=(open)
    open = to_2012(open)
    Rails.logger.debug "OD-#{self.id} set opens_at to #{open}"
    @opens_at = open
    check_wrap
  end

  def opens_at
    return self.open if @opens_at.nil?
    to_2012(@opens_at)
  end

  def closes_at
    return self.close if @closes_at.nil?
    to_2012(@closes_at)
  end
end

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top