#16 Virtual Attributes (revised)
Jul 20, 2012 | 11 minutes | Active Record, Forms
Virtual attributes are a clean way to add form fields that do not map directly to the database. Here I show how to handle validations, associations, and more.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
The validation part is what I looked for a long time, Ryan, thanks very much.
same here
nice sharing
Very nice! How about revising episode #8 "Layouts and Content For" episode?
That'd be really awesome, I've took it for granted it hasn't changed that much, But I'm betting I'm wrong! I too want to see what awesome things you can do with content_for and content_tag
I have to do the price_in_cents stuff all the time, so I made priceable. Nothing too special, but still handy
I realise that you probably want to keep your examples simple and all in one file, but isn't creating categories or tags within the product model considered 'bad form'?
I'd love to see an episode with your take on the current OOP 'movement' in the Rails community - maybe a discussion on how and when you should use POROs etc.
You did some good episodes on design patterns like presenters etc. but a more general look at OOP would be great. Maybe even a multi-part series where you refactor a more complicated real world app?
Keep up the good work.
+1... I am hoping the next episode will be "Refactoring Virtual Attributes (and eliminating callbacks.)" I've been reading up a lot on this and think an episode will solidify the concepts!
Ryan rocks.
+1 here.
When I first started using Rails, the virtual attributes episodes helped me heaps.
But as time goes, I start seeing my model class getting fatter and fatter, and becoming un-manageable (especially with all those AR callbacks). I guess it's a progressive advancement to start using PORO to move the logic out.
+1 for coverage of the Rails OOP movement & how to achieve fast tests
I would really like to see that also.
Btw, don't you guys think that many of these methods could be extracted into a Presenter?
Since they all affect the presentation of the information (they do not modify the data itself), I think would be much better to see it on a Presenter.
The same is not valid to Tag/Category creation, of course.
Thanks!
Not Ryan's take, but Gary Bernhardt at Destroy All Software has some great screencasts along those lines.
Tip: to check if a value is set on a field, Rails provides you with methods that end with a question mark. So your price_in_dollars method will look like this:
It's a small but neat change.
Also, speaking of prices, there is an excellent gem called
money-rails
for handling that: https://github.com/RubyMoney/money-railsI would have liked to see how you handle concatenating multiple fields for the date/time/zone... rails date and time tag helpers are quite clumsy for users to operate. I tend to use text fields for both date and time and a select for time zone, with javascript popups for input selection. I hate the code in my model that puts this all together into a single database datetime column. Do you have a preferred method of handling this type of situation?
I, too, would like to see this. Otherwise, great episode, as always!
First of all: Great episode as always!
But I've got a question:
I tried to use the example from this episode with the creation of a new category in my app. I use internationalization there. Has anybody an idea how I could add the correct label to my text_field, without duplication inside my locales?
Does creating a new category belong in the model? - It seems a bit weird to have it in the model rather than the controller, or is it just me?
I have many price fields (monthly, average, total etc).
Can these getter/setter methods be rewritten for use with all my fields?
Wouldn't it be a good move to make the validation method private?
Virtual attributes don't activate callback functions in Rails 3.2.8
What am I missing? I cannot have a hour and min field convert to a duration model field.
Great Stpes