0

I have a class called Appointment that has, among other attributes, start_time.

In my form, I'm not using start_time directly. I'm separating it into start_time_time (I know it's an awkward name) and start_time_ymd. My goal is to combine start_time_ymd and start_time_time into a complete start_time. (I'm doing this because I don't like the UI for the helper that comes with a date field.)

The error I'm getting is unknown attribute: start_time_time, which is of course not surprising because Appointment doesn't have any attribute called start_time_time.

If start_time_time were something from a related model, I could just do something like accepts_nested_attributes_for :whatever, but since it's not, that wouldn't make sense.

How can I do what I'm trying to do? (I'm a Rails noob so you might have to spoon-feed it to me.)

2 Answers 2

1

I think using a virtual attribute (text version) is your best bet.

Sign up to request clarification or add additional context in comments.

4 Comments

Hmm, that certainly seems like the right idea, but they're not doing quite the same thing as me. They're combining two fields into one and I'm splitting one field into two. It's not possible, as far as I can think, to have to separate setters (start_time_time and start_time_ymd) set the value for one value (start_time). So I'm still unable to solve my problem.
Possibly important: I'm trying to instantiate my Appointment object by saying @appointment = Appointment.new(params[:appointment]). Perhaps I'll have to instantiate a blank object and set each attribute individually in order for this solution to work.
If I set each attribute separately, it worked, and it didn't matter if I had any virtual attributes or not.
The real column is represented as a DateTime object, right? Your virtual attributes can each set certain attributes on the DateTime and not run over each other.
1

You could try this in your model:

attr_accessible :start_time_time, :start_time_ymd

3 Comments

Ah, seems like it was added in rails3. I'm using rails2 atm, and it didn't work.
(I think I was off in context of the question. — Would have to revisit it.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.