attr-accessor

Ruby: dynamically generate attribute_accessor

早过忘川 提交于 2019-12-17 15:43:42
问题 I'm trying to generate the attr_reader from a hash (with nested hash) so that it mirror the instance_variable creation automatically. here is what i have so far: data = {:@datetime => '2011-11-23', :@duration => '90', :@class => {:@price => '£7', :@level => 'all'}} class Event #attr_reader :datetime, :duration, :class, :price, :level def init(data, recursion) data.each do |name, value| if value.is_a? Hash init(value, recursion+1) else instance_variable_set(name, value) #bit missing: attr

ruby pickaxe book says attr_accessor is class method

岁酱吖の 提交于 2019-12-13 19:18:24
问题 In the ruby pickaxe book, there is a line that says attr_accessor is a class method defined in class Module But isn't attr_accessor an instance method? Am I missing something here? 回答1: Yes, all documentation I can find agrees that attr_accessor is an instance method of Module, and I believe it would have to be an instance rather than class method to do what it does. My guess is that it's just a typo. The authors were probably just trying to point out that rather than being part of the

naming methods as variables calling methods Ruby

折月煮酒 提交于 2019-12-13 05:06:47
问题 hi i am very much a beginner. i think i understand how the attr_accessor works (below). and the "setter" is the name=(name) method. and i know that that method is equivalent to the assignment: name = "john" . because "=" is a method that accepts an argument and assigns that argument to whatever object calls it. (though i don't understand how "name" could be considered an object as it is being assigned to an object) so my question is: how can you assign a variable calling a method as a method

Rails 4: Paperclip Error Post model missing required attr_accessor for 'image_file_name'

跟風遠走 提交于 2019-12-12 02:28:00
问题 First, let me specify that I did find two questions on Stack Overflow that are very similar to my problem: Model missing required attr_accessor for 'image_file_name' - Ruby on Rails 4 Paperclip Error: model missing required attr_accessor for 'avatar_file_name' However, I am not sure the issue is the same. Let me explain. In my Rails 4 app, I have the following models: class User < ActiveRecord::Base has_many :administrations has_many :calendars, through: :administrations end class Calendar <

attr_accessor causes Rspec tests to fail

我的梦境 提交于 2019-12-11 03:27:24
问题 I've added a :username_or_email property to my User model as such: class User < ActiveRecord::Base #authlogic acts_as_authentic do |c| c.login_field = :username_or_email end #virtual field for allowing a user to login with their username or email attr_accessor :username_or_email attr_accessible :username, :email, :password, :password_confirmation, :username_or_email validates :username, :presence => true, :length => { :within => 3..20 }, :uniqueness => true, :format => { :with => /\A[a-z0-9]

Order model objects by an attr_accessor

跟風遠走 提交于 2019-12-10 23:27:17
问题 I thought that attr_accessor has the same behavior as the other when I have to sort a list of objects, but it seems that is different: dataRecords = MyData.where("day = ?", Time.now.yesterday.strftime("%Y%m%d").to_i) dataRecords.each do |data| data.accessor_var = func(data.x, data.y) end @sortedData = dataRecords.order('accessor_var DESC') but @sortedData is not being sorted... 回答1: You need to keep in mind that when you apply a scope or order to an ActiveRecord::Relation the data is reloaded

how does the assignment symbol work - Ruby

拜拜、爱过 提交于 2019-12-10 12:59:18
问题 In Ruby if i just assign a local variable. sound = "bang". is that a main.sound=("bang") method? if so, where and how is that method "sound=" being defined? or how is that assignment working? if not, what is actually happening? i know that for a setter method you would say x.sound=("bang"). and you are calling the method "sound=" on the object "x" with the argument "bang". and you are creating an instance variable "sound". and i can picture all of that. but not when you assign a variable in

Rails model attr_accessor attribute not saving?

风格不统一 提交于 2019-12-10 11:33:50
问题 Here is the structure I'm working with: app/models/model.rb class Model < ActiveRecord::Base attr_accessor :some_var end app/models/model_controller.rb class ModelsController < ApplicationController def show @model = Model.find(params[:id]) @other_var if @model.some_var.nil? @model.some_var = "some value" @other_var = "some value" else @other_var = @model.some_var end end end Whenever I run this code (e.g. the show method), the if clause is evaluated to be true (e.g. @model.some_var == nil).

How to delete attr_accessor in ruby

ⅰ亾dé卋堺 提交于 2019-12-08 08:28:29
问题 In ruby, if I have a class greet and has method say_hi that prints out "Hello #{@name}" when name is a instance variable of class greet , and I allow access to @name by adding in the attr_accessor :name , so now I can directly change @name . But how do I remove this attr_accessor if I no longer want people to be able to change @name directly? 回答1: You cannot have a class greet , so I suppose you have something else, say A . class A undef :name undef :name= end 来源: https://stackoverflow.com

Rails model attr_accessor attribute not saving?

落爺英雄遲暮 提交于 2019-12-06 05:13:16
Here is the structure I'm working with: app/models/model.rb class Model < ActiveRecord::Base attr_accessor :some_var end app/models/model_controller.rb class ModelsController < ApplicationController def show @model = Model.find(params[:id]) @other_var if @model.some_var.nil? @model.some_var = "some value" @other_var = "some value" else @other_var = @model.some_var end end end Whenever I run this code (e.g. the show method), the if clause is evaluated to be true (e.g. @model.some_var == nil). How do I get around this? Is there something wrong in my assumption of how attr_accessor works? attr