Questions tagged [virtual-attribute]
113 questions
0
votes
1 answer
How to get ActiveRecord record by virtual attribute?
In Rails 3, how can I retrieve a record by looking up on a virtual attribute? I have a name column and a slug that does a simple modification to it (see code below). How can I do a "reverse lookup" by slug to name?
I'm trying to find the right…

Jim Connors
- 297
- 4
- 14
0
votes
1 answer
STI and virtual attribute inheritance (Rails 2.3)
Say I have an STI relationship, where Commentable is the super class, and NewsComment is the subclass. In Commentable I have:
attr_accessor :opinionated
def after_initialize
self.opinionated = true
end
And in NewsComment:
attr_accessor…

keruilin
- 16,782
- 34
- 108
- 175
0
votes
2 answers
What is the Correct Ruby on Rails Syntax to Write to a Nested Model within a Virtual Attribute?
Trying to divide and conquer these problems (1, 2) I am still having. I would like to write the first step of a BLT recipe in my nested, many-to-many model from the virtual attribute. Later on I would like to have a more complex form, thus I am…

JHo
- 1,068
- 1
- 14
- 29
0
votes
1 answer
Rails 3: Railscast #167 virtual attribute for creating tags - @user.tags
I would like to a tagging system where I can separate the tags by the user's who created them. I followed Railscast #167 about setting up tags using virtual attributes, but that way only lets me call @post.tags to find the tags for a post, but I…

trying_hal9000
- 4,343
- 8
- 43
- 62
0
votes
1 answer
Rails 3 - Process text input to create multiple models
In the app I'm working on, Courses have many Problems, which in turn have many Steps. Right now there is a form for adding Problems to Courses (and then Steps can be added to those problems). What we want is to have a form that just has a field for…

sapphiremirage
- 475
- 2
- 17
0
votes
0 answers
Rails 5 remote form redirect to different pages using virtual attribute
I have a remote: true form in my Rails 5 app and I'd like to redirect to different pages depending on when it's used.
I've tried adding a :redirect_to virtual attribute:
<%= a.input :redirect_to, as: :hidden, input_html: {
value:…

Jeremy Thomas
- 6,240
- 9
- 47
- 92
0
votes
2 answers
How should I access a virtual attribute from an associated model?
I have two models which are associated through has_and_belongs_to_many relationship.
I have created a virtual attribute :full_name from the first model's(User) :first_name and :last_name attributes.
I am using ActiveAdmin, and trying to show the…

dbate
- 127
- 13
0
votes
2 answers
Rails - Virtual attribute in form helper
I'm having an odd error with a virtual attribute in a form helper.
My model looks like this:
class Folder < ActiveRecord::Base
...
# VIRTUAL ATTRIBUTES
def parent_name
self.parent.name
end
def parent_name=(name)
self.parent =…

Andrew
- 42,517
- 51
- 181
- 281
0
votes
2 answers
Rails virtual attribute in form not updating db from form or console
So in my Rails web application I have a section of code that prompts underage users to input a parent/guardian email when creating an account. In my database I would like to store this as the corresponding ID but naturally the user will want to…

Brian
- 7
- 4
0
votes
1 answer
Calculate And Display A Date Using A Virtual Attribute in Rails 4
I'm trying to calculate and display a date using a virtual attribute in Rails. I have a model, Group, and each Group has several GroupMeetings. There is also a GroupDates table that stores the first start date of each Group (submitted via a form by…

ev662
- 69
- 1
- 10
0
votes
1 answer
Rails 5 Constant & Model & Virtual Attributes
I am trying to use predefined constant in Model virtual attributes.
when I create user, it shows absolute avatar_url and works fine.
Problem:
when I find user in login method it return only relative url i.e "avatar_url":…

Kaleem Ullah
- 6,799
- 3
- 42
- 47
0
votes
2 answers
Yii2 gridview filter virtual attribute textfield showing a number by default
Model:
public $FnGdiff;
public function getFnGdiff() {
return $this->FnG - $this->fd;
}
ModelSearch:
public function rules() {
return [
[['fnGdiff'], 'safe'],
];
}
now if I add fnGdiff to gridview, there is always a number(?!)…

user2511599
- 796
- 1
- 13
- 38
0
votes
1 answer
after_initialize virtual attribute
I am unable to assign a value to a virtual attribute in the after_initialize method:
attr_accessor :hello_world
def after_initialize
self[:hello_world] = "hello world"
end
In my view file:
hello world defaulted? <%= @mymodel.hello_world…

amaseuk
- 2,147
- 4
- 24
- 43
0
votes
1 answer
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…

holden
- 13,471
- 22
- 98
- 160
0
votes
1 answer
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…

Satchel
- 16,414
- 23
- 106
- 192