Tag: 访问器

覆盖默认访问者时更新属性的麻烦

我正在使用Ruby on Rails 4,我用这种方式覆盖了一些默认的访问器方法: class Article < ActiveRecord::Base def title self.get_title end def content self.get_content end end self.get_title和self.get_content方法返回一些计算值,如下所示(注意: has_one_association是a :has_one ActiveRecord::Association ) def get_title self.has_one_association.title.presence || read_attribute(:title) end def get_content self.has_one_association.content.presence || read_attribute(:content) end 当我从数据库中找到并读取@article实例时,所有实例都按预期工作: title和content值分别使用self.has_one_association.title和self.has_one_association.content输出。 但是,我发现当属性分配给@article ,@ @article对象不会按预期更新。 也就是说,在我的控制器中,我有: def update # params # => {:article => {:title => “New title”, :content => “New […]

Rails – 添加不在模型和更新模型属性中的属性

我的表单中有3个字段,不在我的数据库中:opening_type,opening_hours,opening_minutes。 我想用这3个字段更新主要属性“打开”(在数据库中)。 我尝试了很多不起作用的东西。 其实我有: attr_accessor :opening_type, :opening_hours, :opening_minutes def opening_type=(opening_type) end def opening_type opening_type = opening.split(“-“)[0] if !opening.blank? end def opening_hours=(opening_hours) end def opening_hours opening_hours = opening.split(“-“)[1] if !opening.blank? end def opening_minutes=(opening_minutes) end def opening_minutes opening_minutes = opening.split(“-“)[2] if !opening.blank? end 我尝试添加类似的东西: def opening=(opening) logger.info “WRITE” if !opening_type.blank? and !opening_hours.blank? and opening_minutes.blank? opening = “” […]

在Rails模型中设置和获取虚拟属性

我正在寻找一种方法来解决以下问题: Event模型中的两个datetime属性: start_at: datetime end_at: datetime 我想使用3个字段以表格forms访问它们: event_date start_time end_time 我遇到的问题是如何将实际和虚拟属性保持在“同步”,以便可以通过表单和/或直接通过end_at和end_at更新模型。 class Event < ActiveRecord::Base attr_accessible :end_at, :start_at, :start_time, :end_time, :event_date attr_accessor :start_time, :end_time, :event_date after_initialize :get_datetimes # convert db format into accessors before_validation :set_datetimes # convert accessors into db format def get_datetimes if start_at && end_at self.event_date ||= start_at.to_date.to_s(:db) # yyyy-mm-dd self.start_time ||= "#{'%02d' […]

为什么attr_accessor在Ruby on Rails中破坏了这个模型中的现有变量?

最近我被这种情况所困扰,确切地知道发生了什么事情是有用的,所以其他人避免这种错误。 我有一个模型用户,其架构如下: create_table “users”, :force => true do |t| t.string “user_name” t.string “first_name” t.string “last_name” t.string “email” t.string “location” t.string “town” t.string “country” t.string “postcode” t.boolean “newsletter” 在类user.rb中,我有三个方法的attr_accessor: class User < ActiveRecord::Base # lots of code attr_protected :admin, :active # relevant accessor methods attr_accessor :town, :postcode, :country end 现在在我的用户控制器中,如果我有以下方法: def create @user = User.new params[:user] […]

Rails:attr_accessor和attr_accessible之间的区别

有什么不同? 另外,为什么这不起作用: 未设置base_path等变量。 class Cvit < ActiveRecord::Base attr_accessible :species,:program,:textup,:e_value,:filter,:min_identity,:cluster_dist,:fileup_file_name attr_accessor :base_path, :fa_file, :text_file, :dbase, :source, :bl_file, :bl_sorted, :gff_file, :cvt_file, :db, :overlay_coords_gray def initilize(*args) super(*args) end def cvitSetup() self.base_path = "blast_cvit/" self.fa_file = "input.fa" . . end end 在rails控制台中,属性设置正确但是当我尝试这样做时: 控制器: def show @cvit = Cvit.find(params[:id]) @cvit.cvitSetup() @cvit.blast() @cvit.generateGff() @cvit.generateCvitImage() respond_to do |format| format.html # show.html.erb format.xml […]

在一个类中混合attr_accessor和initialize方法

我看到代码如下: class Person def initialize(name) @name = name end end 我理解这允许我做像person = Person.new这样的事情,并像在其他方法中一样在我的类中使用@name 。 然后,我看到代码如下: class Person attr_accessor :name end … person = Person.new person.name = “David” 我只是对这两种方法网格感到茫然。 def initialize(name)的具体用途是什么? 我想attr_accessor允许我读写。 这意味着它们是两种不同的方法。 是? 想要对def initialize和attr_accessor以及它们如何进行网格def initialize澄清。

为什么Ruby中的符号不​​被认为是一种变量?

编程和Ruby的新手,我希望这个关于符号的问题符合要求。 我理解Ruby中的符号(例如:book , :price )特别适合作为散列键,并且可以全面地完成字符串可以执行的轻量级特定子集。 但是,我在某方面对符号感到困惑。 具体来说,当它们在attr_accessor类型的方法中使用时,它们看起来更像是一个变量。 例如, attr_reader :book, :price 。 如果它们是该用法中的变量,那么这有点令人费解,因为它们通常不在变量类型中列出(如$ global,@ instance,local,@@ class,有时候,CONSTANT,变量类型)被描述。 如果符号是以这种方式使用的变量,那么应该对它们有什么范围? 或者它们在这种情况下仍然是某种轻量级的字符串? (或者也许以更广泛的方式,符号,字符串和变量都具有基本的鸭子性质?)提前感谢您的见解和建议。