virtual-attribute

Signaling validation errors in assigning a virtual attribute?

This is a Rails/ActiveRecord question. I have a model which basically has to represent events or performances. Each event has many attributions: an attribution is basically something like "In this event, Person X had Role Y". I concluded that the best way to allow a user to edit this data is by providing a free text field which expects...

How do I validate a virtual attribute in an ActiveRecord model?

I am using virtual attributes to save tags in an Entry model from a comma-separated textbox in my form (based on Railscasts #167): class Entry < ActiveRecord::Base has_many :entry_tags has_many :tags, :through => :entry_tags after_save :update_tags attr_writer :tag_names def tag_names tags.map(&:name).join(", ") end ...

ActiveRecord association getting saved before the record in an update

I have an Entry model which has many Tags. Tags are added to an entry by typing them into a textbox on my form, via a tag_names virtual attribute. Before validation on the Entry model, the tag_names string is converted into actual Tag objects using find_or_create_by_name. The Tag model also has validation to make sure the tag name matche...

Virtual attributes in plugin

Hello, I need some help with virtual attributes. This code works fine but how do I use it inside a plugin. The goal is to add this methods to all classes that uses the plugin. class Article < ActiveRecord::Base attr_accessor :title, :permalink def title if @title @title elsif self.page self.page.title else...

Rails virtual attribute search or sql combined column search

Hello All! I have a user model with attributes 'first' and 'last' So for example User.first.first #=> "Charlie" User.first.last #=> "Brown" This User model also has a virtual attribute 'full_name' #user.rb def full_name [first,last].join(' ') end def full_name=(name) #don't know what to do with people w/ middle names split = name...

Rails Validating Virtual Attributes

My model looks like this: class Item < ActiveRecord::Base has_many :locations validate :validate_item_location def item_location locations.address+','+locations.city+','+locations.country end def item_location=(str) geo = Geokit::Geocoders::MultiGeocoder.geocode(str) if geo.success locations.build( :lat => ...

Virtual attributes on new models in Rails?

Hi, This certain part of my application takes cares of creation of Webshop model for store chains (like H&M) that has one. If the chain has a website that also is a webshop it creates one Webshop model. If the website is not a webshop then it lets it be just at string in the Chain model. PROBLEM: I'm doing this with a checkbox and vir...

Preference values - static without tables using a model with virtual attributes

Im trying to eliminate two tables from my database. The tables are message_sort_options and per_page_options. These tables basically just have 5 records which are options a user can set as their preference in a preferences table. The preferences table has columns like sort_preferences and per_page_preference which both point to a record ...

How to create read-only virtual attributes in Ruby models

I would like to create a virtual attribute that will always be included when you do model_instance.inspect. I understand that attr_reader will give me the same thing as just defining an instance method, but I would like this attribute to be part of the object's "make up" How can I accomplish this? Thanks! UPDATE Here is what is not ...

Initialize virtual attributes

I have an IncomingEmail model with an attachments virtual attribute: class IncomingEmail < ActiveRecord::Base attr_accessor :attachments end I want the attachments virtual attribute to be initialized to [] rather than nil so that I can do: >> i = IncomingEmail.new => #<IncomingEmail id: nil,...) >> i.attachments << "whatever" W...

How can I get a unique :group of a virtual attribute in rails?

I have several similar models ContactEmail, ContactLetter, etcetera. Each one belongs_to a Contact Each contact belongs_to a Company So, what I did was create a virtual attribute for ContactEmail: def company_name contact = Contact.find_by_id(self.contact_id) return contact.company_name end Question: How can I get an...

Can I count based on virtual attribute?

I have the following error: no such column: company_name: SELECT count("contact_emails".id) AS count_id FROM "contact_emails" The model ContactEmail does NOT have a column company_name. I created it as a virtual attribute. It's not possible to do a select based on that information? How would I do it, then? ContactEmail belongs_to...

virtual attribute with dates.

I have a form which i'd like to simplify. I'm recording a startdate and an enddate, but would like to show the user only a startdate and then a drop down with number of days. But I'm having problems with my model and storing it correctly. The first part works. def date=(thedate) #puts the startdate in the correct format... ...