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...
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
...
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...
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...
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...
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 => ...
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...
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 ...
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 ...
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...
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...
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...
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...
...