Questions tagged [virtual-attribute]
113 questions
3
votes
1 answer
Rails: How to make available parent attribute in setter method
Context:
I have a company model with many projects, having many tasks. The company also has many employees, which in turn have many tasks.
Schema:
Problem:
I'm building a form to create a project where the user can add multiple tasks. Upon…

Matthias
- 1,884
- 2
- 18
- 35
3
votes
1 answer
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…

Jogloran
- 31
- 2
3
votes
1 answer
Virtual Attribute in Rails 4
I have a model of product and I need to write in the _form view , the number of the product that an admin wants to insert.
I have another table with the Supply (number of product)
so in my product table I don't have the attribute quantity , but I…

Vito
- 746
- 2
- 9
- 25
3
votes
2 answers
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…

Daniel Vandersluis
- 91,582
- 23
- 169
- 153
2
votes
1 answer
passing current_user to model via callback
I have a couple of callbacks that log activity on create/update:
class Projelement
..
after_create { |p| p.log_projelement_activity "created" }
after_update { |p| p.log_projelement_activity "edited" }
I need to augment the design to pass…

Daniel May
- 2,192
- 5
- 25
- 43
2
votes
1 answer
Rails 3 virtual attributes used in another Models controller
Experimenting an issue trying to achieve a virtual attributes of a model in another controller.
Is there a way to do it?
Here is the virtual attributes:
def montant
self.facture_details.sum(:montant_detail)
end
def paiements
…

Dannoel
- 161
- 2
- 12
2
votes
1 answer
accepts_nested_attributes_for virtual attributes
I have 2 models:
class Invoice < ActiveRecord::Base
has_many :invoice_items
accepts_nested_attributes_for :invoice_items, :allow_destroy => true
end
class InvoiceItem < ActiveRecord::Base
attr_accessor :encryption_key
…

Paul Barclay
- 489
- 6
- 16
2
votes
2 answers
Is is possible to return virtual attributes automatically in Rails models?
Given:
class Foo
has_one :bar
def bar_name
bar.name
end
end
class Bar
belongs_to :foo
end
In the console or in a view, I can @foo.bar_name to get 'baz'.
I'm aware that I can @foo.as_json(methods: :bar_name) to get {"id"=>"abc123",…

Micah Alcorn
- 2,363
- 2
- 22
- 45
2
votes
2 answers
sum() or group() virtual attributes in ruby on rails
let's say I have a model like this:
class Surface < ActiveRecord::Base
attr_accessible :width, :length
def area
self.width * self.length
end
end
Where @surface.area returns the area of a surface. Now, lets say I have many surfaces…

kernification
- 511
- 1
- 5
- 15
2
votes
1 answer
Rails virtual attribute with ActiveRecord
I have a MySQL database table with the following simplified schema:
create_table "sensors", force: :cascade do |t|
t.integer "hex_id", limit: 8, null: false
end
where hex_id is declared as a BIGINT in MySQL. I would like for the user to…

Vadim
- 1,916
- 2
- 19
- 39
2
votes
1 answer
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…

Tom Lehman
- 85,973
- 71
- 200
- 272
2
votes
1 answer
rails simple_form using virtual attributes
I am trying to incorporate Simple_forms into my app using a virtual attributes. I am following http://railscasts.com/episodes/102-auto-complete-association-revised to get autocomplete to work as well. Simple_form works when i do not use a virtual…

Mike
- 25
- 4
2
votes
0 answers
Rails: is it possible to define virtual attribute through associations?
I have two models:
Skillnames and Skills
User can create skillname and skill, in skills model I have a reference "skill_id" which means when I do Skillname.where(:id => @skill.skill_id) it will give me back the name.
Now I'm super confused over…

0bserver07
- 3,390
- 1
- 28
- 56
2
votes
2 answers
Single virtual attribute definition for multiple fields
I'm wondering if there is a way to set a virtual attribute so that it can handle multiple fields/variables.
Basically, I have several different integer columns in my model (income, taxes) and for each of them I need to check when the form is…

Jon Girard
- 245
- 1
- 4
- 15
2
votes
1 answer
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 =…

xpepermint
- 35,055
- 30
- 109
- 163