virtual-attribute

Rails 4 Minitest Testing Virtual Attributes

邮差的信 提交于 2020-01-17 05:38:22
问题 So I have a class TimeBank, and its being updated, and is getting two new virtual attributes which will manipulate two other existing attributes (DB columns). class TimeBank < ActiveRecord::Base # validations, consts, few other methods omitted for brevity def date_worked @date_worked ||= self.start.to_date rescue Date.current end def date_worked=(date_worked_val) self.attributes['date_worked'] = date_worked_val end def hours_worked @hours_worked ||= hours rescue NoMethodError nil end def

How to access virtual attributes of model in a nested form with Rails

老子叫甜甜 提交于 2020-01-05 08:10:19
问题 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 => true ... Model 2: Transaction belongs_to :lease def balance_to_date(aDate) #Returns the current balance up to aDate ... end ... In the nested form I want to put something like: <td style="width:100px;"><%= nested_f.text_field :dated, size: 8 %> </td> <td

rails virtual attributes won't read from form_for submission

爷,独闯天下 提交于 2019-12-23 03:32:18
问题 I am trying to implement a rails tagging model as outlined in Ryan Bate's railscast #167. http://railscasts.com/episodes/167-more-on-virtual-attributes This is a great system to use. However, I cannot get the form to submit the tag_names to the controller. The definition for tag_names is : def tag_names @tag_names || tags.map(&:name).join(' ') end Unfortunately, @tag_names never gets assigned on form submission in my case. I cannot figure out why. SO it always defaults to tags.map(&:name)

Rails virtual attribute search or sql combined column search

倖福魔咒の 提交于 2019-12-17 18:56:27
问题 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" #=>

Single virtual attribute definition for multiple fields

China☆狼群 提交于 2019-12-12 16:32:54
问题 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 submitted, and remove illegal characters, which I'm doing by setting a virtual attribute and using tr to strip the characters. def flexible_income income end def flexible_income=(income) self.income = income.tr('$ ,', '') unless income.blank? end And then

Rails: How to make available parent attribute in setter method

試著忘記壹切 提交于 2019-12-12 09:52:15
问题 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 submission the form should create a single project record, one or more task records with hours and employee_id attributes and (!) check if the employee name already exists in the database or create a new one. I've approached this by adding jquery

Yii2 non-DB (or virtual) attribute isn't populated during massive assignment?

百般思念 提交于 2019-12-12 02:59:12
问题 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 populated along with DB attributes? Or am I doing something wrong that is not populated however it should be? Thanks! 回答1: Docs: Massive Assignments Like normal models, Active Record instances also enjoy the massive assignment feature. Using this feature,

yii sort by a custom attribute

£可爱£侵袭症+ 提交于 2019-12-11 09:59:59
问题 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: <?php $this->widget('zii.widgets.grid.CGridView', array( ... array( 'name' => 'count', 'value' => '$data->getVacPeriod()' ), ... ), )); ?> it works fine. but I don't know how can I sort upon this custom attribute. I tried to use CSort but it does not work. any idea? 回答1: To use CSort for

Rails 3 config setting for attr_accessible/protected

左心房为你撑大大i 提交于 2019-12-10 18:38:00
问题 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 endeavor by adding it to attr_accessible in the first place. To keep this from happening again, is there a configuration setting I can flag to throw an exception on development if I try to mass assign something and validate it when it is protected

Rails: How to make available parent attribute in setter method

邮差的信 提交于 2019-12-05 22:05:16
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 submission the form should create a single project record, one or more task records with hours and employee_id attributes and (!) check if the employee name already exists in the database or create a new one. I've approached this by adding jquery autocomplete to the form and defining a virtual attribute with a getter and setter method in my Task model.