Questions tagged [virtual-attribute]
113 questions
25
votes
3 answers
What would a default getter and setter look like in rails?
I know that I can write attr_accessor :tag_list to make a virtual attribute tag_list for an object in Rails. This allows there to be a tag_list attribute in forms for the object.
If I use attr_accessor :tag_list I can, in the model, perform actions…

jay
- 12,066
- 16
- 64
- 103
11
votes
2 answers
Rails virtual attribute search or sql combined column search
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…

Nik So
- 16,683
- 21
- 74
- 108
10
votes
3 answers
Rails 3: creating a validation with :if statement
Hi I'm trying to setup a validation that is only called in a specific form view, to do this I'm trying to create a hidden_field for a virtual attribute on the form and set this to a value, then validate :if the virtual attribute equals the…

trying_hal9000
- 4,343
- 8
- 43
- 62
10
votes
1 answer
Virtual attributes in rails 4
How can I use virtual attributes(getter, setter) in rails 4, as 'attr_accessible' removed.
I am getting issue, here
def tags_list
@tags = self.tags.collect(&:name).join(', ')
end
I can reach above method, but not able to reach setter…

Ashwin Yaprala
- 2,737
- 2
- 24
- 56
9
votes
4 answers
In Yii framework how can I Combine columns and show as display string in dropdownlist
I have a dropDownList in my view, it is populating from clients table, the table contains columns like first_name, last_name,id etc., Now I want to show the first_name and last_name as display text and id as value in drop down list, I'm done with id…

arun
- 3,667
- 3
- 29
- 54
6
votes
3 answers
Virtual attribute not moved to the model hash inside params
I'm having a problem in my Rails 3.2 app where a virtual attribute sent restfully via JSON is not in the right place in the params hash. Well, it isn't where I expect. It remains to be seen if my expectations are correct. :)
I have a model using the…

Tetious
- 123
- 6
6
votes
2 answers
Setting & getting virtual attributes in Rails model
I'm looking for a rails-y way to approach the following:
Two datetime attributes in an Event model:
start_at: datetime
end_at: datetime
I would like to use 3 fields for accessing them in a form:
event_date
start_time
end_time
The problem I'm…

Meltemi
- 37,979
- 50
- 195
- 293
5
votes
3 answers
When to use a virtual attribute or pass data in a hash in Rails 3
In my application, I have a Widget model, a Feature model with a has_many through association by WidgetFeature table.
As per the requirements, when I am sending a WidgetFeature object, I should append the feature_name to it as well, for the given…

user738184
- 111
- 4
5
votes
1 answer
rails 3 find or create based on multiple fields
I am trying to implement a simple tagging system using a tag virtual attribute on a notes object. a tag contains a label and a user_id. what I would like to do is update the HABTM to relationship to the note based on what is in the tags field. I…

GSto
- 41,512
- 37
- 133
- 184
5
votes
1 answer
Yii2 add related attribute to sort
I have normal ModelSearch with ActiveDataProvider, and I would like to add a virtual/related attribute to sorting in gridview. If I'm doing with setSort, and I'm adding this only attribute, then all other attributes are not sortable any more. Is…

user2511599
- 796
- 1
- 13
- 38
4
votes
2 answers
Virtual attributes Rails - Unknown Attribute Error
I'm making a Rails 4 application. Each incident belongs to a user. Each incident has many events. I want that I can create an Incident with current_user.incidents.new and pass it an attribute message which exists in the Event model. On creation, I…

Umang Raghuvanshi
- 1,228
- 15
- 34
4
votes
2 answers
Selecting Rails Models via Virtual Attributes
I have the two rails models Section & SectionRevision. A Section is mostly just a container that holds all the Revisions relating to itself. So most of the attributes for the Section are basically stored within the SectionRevision model so there's a…

Ryan King
- 3,538
- 12
- 48
- 72
4
votes
3 answers
How can I add a derived column to my Yii model?
In Yii, I need to add a "derived" column to any resultset from my model. The column doesn't actually exist in the database table.
For example, say I have an Activity model. There are only two types of Activities: (1) Income, or (2) Expense.
If I…

Turgs
- 1,729
- 1
- 20
- 49
3
votes
1 answer
Rails 3 config setting for attr_accessible/protected
I just spent quite some time trying to resolve a virtual attribute issue in my model. It turned out I'd simply forgotten to add it to attr_accesible in my model. Granted I should have caught it earlier or better should have started the whole…

Yuri
- 1,261
- 1
- 11
- 23
3
votes
1 answer
Virtual attribute validation Rails 3
I have not done a whole lot with this Jquery-tokeninput or Rails virtual attributes but have been slamming my head against the wall with this one. Any help or guidance is appreciated.
I have a virtual attribute reader in my announcement model which…

Yuri
- 1,261
- 1
- 11
- 23