attr-accessor

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

understand self for attr_accessor class method

╄→尐↘猪︶ㄣ 提交于 2019-12-04 01:24:42
问题 class Test class << self attr_accessor :some def set_some puts self.inspect some = 'some_data' end def get_some puts self.inspect some end end end Test.set_some => Test puts Test.get_some.inspect => Test nil Here above I could find self as Test itself but not returning the some_data as output. But while I modified in following way it returns expected output class Test class << self attr_accessor :some def set_some puts self.inspect self.some = 'some_data' end def get_some puts self.inspect

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

Cannot access attr_accessor defined variables

半城伤御伤魂 提交于 2019-12-01 17:14:49
问题 I am using Thinking Sphinx to run searches and I get the appropriate ActiveRecord Models fine. The problem is, I want to create an appropriate link path and text on each model, then send the info to the browser in the form of JSON, via AJAX. I am using the following to build those link attributes: In the controller: class FindController < ApplicationController def tag_results @results = ThinkingSphinx.search(params[:terms]) @results.each do |result| result.build_ajax_response end respond_to

understand self for attr_accessor class method

别等时光非礼了梦想. 提交于 2019-12-01 05:19:37
class Test class << self attr_accessor :some def set_some puts self.inspect some = 'some_data' end def get_some puts self.inspect some end end end Test.set_some => Test puts Test.get_some.inspect => Test nil Here above I could find self as Test itself but not returning the some_data as output. But while I modified in following way it returns expected output class Test class << self attr_accessor :some def set_some puts self.inspect self.some = 'some_data' end def get_some puts self.inspect self.some end end end Test.set_some => Test puts Test.get_some.inspect => Test some_data What is the

Ruby instance_eval on a class with attr_accessor

这一生的挚爱 提交于 2019-12-01 04:29:11
问题 I understand the basic difference between instance_eval and class_eval . What I've discovered though when playing around is something strange involving attr_accessor . Here's an example: A = Class.new A.class_eval{ attr_accessor :x } a = A.new a.x = "x" a.x => "x" # ... expected A.instance_eval{ attr_accessor :y } A.y = "y" => NoMethodError: undefined method `y=' for A:Class a.y = "y" => "y" # WHATTT? How is it that: the instance_eval didn't at the accessor onto our A class (object) it then

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

How do I set an attr_accessor for a dynamic instance variable?

喜你入骨 提交于 2019-11-29 20:56:49
I dynamically created an instance variable within my class: class Mine attr_accessor :some_var def intialize @some_var = true end def my_number num self.instance_variable_set "@my_#{num}", num end end How do I make @my_#{num} now as an attr value? e.g. I want to be able to do this: dude = Mine.new dude.my_number 1 dude.my_1 => 1 this answer doesn't pollutes the class space, example.. if i do mine.my_number 4 then the other instances of Mine will not get the my_4 method.. this happens because we use the singleton class of the object instead of the class. class Mine def my_number num singleton

Mailer unable to access reset_token in User model

懵懂的女人 提交于 2019-11-29 16:16:53
faced with an issue where @user.reset_token returns nil. app/views/user_mailer/password_reset.html.erb <%= link_to "Reset password", edit_password_reset_url(@user.reset_token, email: @user.email) %> Reset_token is declared in User model, whereby this problem happens when I try to use a sidekiq worker. Refer to code below. app/models/user.rb class User < ActiveRecord::Base attr_accessor :reset_token def User.new_token SecureRandom.urlsafe_base64 end def send_password_reset_email PasswordResetWorker.perform_async(self.id) end private def create_reset_digest self.reset_token = User.new_token

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