virtual-attribute

Setting & getting virtual attributes in Rails model

有些话、适合烂在心里 提交于 2019-12-04 22:13:08
问题 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 having is how to keep the actual and the virtual attributes in "sync" so the model can be updated via the form and/or directly via start_at & end_at . class Event < ActiveRecord::Base attr_accessible :end_at, :start_at, :start_time, :end_time, :event_date

Virtual attributes in rails 4

这一生的挚爱 提交于 2019-12-04 11:12:49
问题 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 below, when trying to update/create. def tags_list=(tags) @tags = tags end 回答1: Using virtual attributes in Rails 4 pretty much the same as with attr_accessible. You just have to add your virtual attribute to the permitted params in your controller (instead of

In Yii framework how can I Combine columns and show as display string in dropdownlist

假如想象 提交于 2019-12-03 12:38:19
问题 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 as value and first_name as display text, but here I want to combine those columns ( first_name and last_name ) and use as display text. in model function getClients() { $Clients = Client::model()->findAll(); $list = CHtml::listData($Clients ,

Virtual attributes in rails 4

ぃ、小莉子 提交于 2019-12-03 06:11:29
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 below, when trying to update/create. def tags_list=(tags) @tags = tags end Using virtual attributes in Rails 4 pretty much the same as with attr_accessible. You just have to add your virtual attribute to the permitted params in your controller (instead of attr_accessible), then add the getter and setter methods as usual in your model. # your_controller.rb private

In Yii framework how can I Combine columns and show as display string in dropdownlist

隐身守侯 提交于 2019-12-03 03:06:01
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 as value and first_name as display text, but here I want to combine those columns ( first_name and last_name ) and use as display text. in model function getClients() { $Clients = Client::model()->findAll(); $list = CHtml::listData($Clients , 'client_id', 'first_name'); return $list; } in view echo $form->dropDownList($model,'client_id',$model-

What would a default getter and setter look like in rails?

*爱你&永不变心* 提交于 2019-11-30 06:36:57
问题 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 on tag_list to pull and manipulate data from the form. What I want to know is, instead of writing attr_accessor, how would I write a getter and setter that would replicate completely the default functionality of attr_accessor. EG: def tag_list

What would a default getter and setter look like in rails?

不羁岁月 提交于 2019-11-28 20:23:30
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 on tag_list to pull and manipulate data from the form. What I want to know is, instead of writing attr_accessor, how would I write a getter and setter that would replicate completely the default functionality of attr_accessor. EG: def tag_list #what goes here end FYI I have tried def tag_list @tag_list end This does NOT work. attr_accessor is a

Rails virtual attribute search or sql combined column search

自闭症网瘾萝莉.ら 提交于 2019-11-28 09:14:58
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.split(' ') self.first = split[0] self.last = split[1] end So for example: User.first.full_name = "Charlie Brown" #=> "Charlie Brown" User.first.full_name = "Homer Simpson" #=> "Home Simpson" User.first.save User.first.first #=> "Homer" User.first.last #=> "Simpson" It'd be so