Questions tagged [virtual-attribute]
113 questions
1
vote
1 answer
How are virtual attributes managed in memory?
I'm going through Michael Hartl's rails tutorial and have come to a point where he defines a virtual attribute, remember_token and later assigns a value to it in a remember instance method:
class User < ApplicationRecord
attr_accessor…

Carl Edwards
- 13,826
- 11
- 57
- 119
1
vote
2 answers
Yii2 virtual attribute getter had to change to function call
I'm new to OOP and Yii2. I have a function in Model:
public function getDatRev() {
if ($this->rev) {
return $this->rev;
} else {
return $this->datum;
}
}
in the View until now I have used it like…

user2511599
- 796
- 1
- 13
- 38
1
vote
2 answers
update value of virtual attribute in view is not updating model
I'm updating the value of a virtual attribute in my view but this is not being reflected in the model instance.
For example:
MyModel.erb
attr_accessor_with_default :hello_world, false
View.html.erb
<%= @mymodel.hello_world %>
<% if (…
<% if (…

amaseuk
- 2,147
- 4
- 24
- 43
1
vote
1 answer
Yii2: How to order a relation by virtual attribute?
I have a class Group, which hasMany Teams and I would like to order them by their score. A team's score is calculated by iterating through its games, so score is not a db-field, it's a virtual read-only attribute.
This is my attempt:
public function…

Mathias
- 334
- 3
- 5
- 22
1
vote
1 answer
Yii2 non-DB (or virtual) attribute isn't populated during massive assignment?
I've defined a virtual attribute:
class ContactForm extends Model {
public $name; // is not a DB field
I've noticed that it is not populated during massive assignment (after submitting the form, in $model->load($_POST)). Can it be somehow…

user2511599
- 796
- 1
- 13
- 38
1
vote
1 answer
Treating the file store as a column in the table
I have a Blog model of which content will be stored in a file and the remaining things will be stored in the table.
My solution for this is as below
I created a virtual attribute with getter/setter method.
def content
…

Nitin Satish Salunke
- 508
- 6
- 16
1
vote
3 answers
How to refactor duplicated virtual attributes?
I've run into a problem where I have virtual attributes within models that are very similar. In short, they are acting as "converters" for some of the attributes. Here is an example of some of these virtual attributes:
class Setting <…

benbartling
- 23
- 3
1
vote
1 answer
How can I define virtual properties in node-orm?
I'm trying to set up a users table with node-orm, and I want to define password & passwordConfirmation as virtual properties (i.e.: not saved, just used for validation & computation) as well as encryptedPassword, which actually gets saved to the DB.…

Matt Huggins
- 81,398
- 36
- 149
- 218
1
vote
1 answer
Raise validation error from virtual attribute in Rails
I got two models: Source and SourceType. Source of course belongs to SourceType.
I want to create new source and assign proper sourcetype object to it. 'Proper' means that one virtual attribute of the source object match some of sourceType objects…

Roaring Stones
- 1,054
- 7
- 22
1
vote
1 answer
Rails 4: composed_of mapping to JSON store attribute
I have the following models set up
# task.rb
class Task << AR
# everything all task objects have in common
end
# login_request.rb
class Tasks::LoginRequest < Task
store :data, accessors: [:email, :first_name, :last_name, :expires_at]
…

Vapire
- 4,568
- 3
- 24
- 41
1
vote
1 answer
What are the caveats of using 'isMemberOf' virtual attribute to determine a user's group membership?
Do all LDAP servers support this virtual attribute (by default)?
I read that we have to enable this virtual attribute before using it. Is this how it works for all LDAP servers? Isn't this enabled by default?
Are there any other drawbacks of using…

coder
- 317
- 1
- 7
- 20
1
vote
1 answer
How to access virtual attributes of model in a nested form with Rails
I have a basic nested form. I want to access a virtual attribute for the nested form's model.
Model 1: Lease
Has_many :transactions
accepts_nested_attributes_for :transactions, :reject_if => lambda { |a| a[:dated].blank? }, :allow_destroy =>…

nevieandphil
- 390
- 4
- 11
1
vote
4 answers
Get current_user in Rails form validation by defining a virtual attribute
Rails form validation is designed to go in the model most easily. But I need to make sure the current user has the required privileges to submit a post and the current_user variable is only accessible in the controller and view.
I found this answer…

alt
- 13,357
- 19
- 80
- 120
1
vote
1 answer
yii sort by a custom attribute
In my Vacation model Vac I have this function
public function getVacCount(){
this function returns how many days there are in one vacation.
and I want to add a custom column to the cgridview like this:

Aladdin Mhemed
- 3,817
- 9
- 41
- 56
1
vote
1 answer
Virtual attribute is not set before attr_encrypted uses said virtual attribute for encryption key
When encryption_key is called by attr_encrypted, :passphrase hasn't been set. The encryption key ends up being a sha1 hash of the salt; it should be a sha1 hash of the passphrase and salt.
The salt is generated on creation and saved in the…

pdjk
- 13
- 2