jjzjj

attribute_accessor

全部标签

ruby-on-rails - 为没有 `attr_accessor` 的 Rails 模型设置非数据库属性

在PHP中,我可以为模型设置一个属性(不是数据库中的列)。例如(PHP代码),$user=newUser;$user->flag=true;但在Rails中,当我设置数据库中不存在的任何属性时,它会抛出错误undefinedmethodflag。有attr_accessor方法,但是如果我需要大约十个临时属性会怎样? 最佳答案 butwhatwillhappenifIneedabouttentempattributes?#app/models/user.rbclassUserattr_accessor创建"virtual"attri

ruby - 使用 attr_accessor 动态创建类属性

在Ruby中,有没有办法动态地向类中添加实例变量?例如:classMyClassdefinitializecreate_attribute("name")enddefcreate_attribute(name)attr_accessorname.to_symendendo=MyClass.newo.name="Bob"o.name 最佳答案 一种方法(还有其他方法)是这样使用instance_variable_set和instance_variable_get:classTestdefcreate_method(name,&bloc

ruby-on-rails - Ruby on Rails : How to validate nested attributes on certain condition?

我有这些模型:classOrganisation:addressable,:dependent=>:destroyaccepts_nested_attributes_for:address,:allow_destroy=>trueendclassPerson:addressable,:dependent=>:destroyaccepts_nested_attributes_for:address,:allow_destroy=>true#Thesetwomethodsseemtohavenoeffectatall!validates_presence_of:organisation,:

Ruby attr_accessor 与 getter/setter 基准测试 : why is accessor faster?

我刚刚针对等效的getter/setter方法测试了attr_accessor:classA#wedefinetwoR/Wattributeswithaccessorsattr_accessor:acc,:bcc#wedefinetwoattributeswithgetter/setter-functionsdefdirA=(d);@dirA=d;enddefdirA;@dirA;enddefdirB=(d);@dirB=d;enddefdirB;@dirB;endendvarA=A.newstartT=0dirT=0accT=0#nowwedo100timesthesamebench

ruby-on-rails - Rails 4 强参数 : can I 'exclude' /blacklist attributes instead of permit/whitelist?

我正在将Rails3应用程序迁移到Rails4,并且正在将attr_accessible属性转换为Controller中的强参数。APIDocumentation显示如何“允许”属性:defperson_paramsparams.require(:person).permit(:name,:age)end然而,我的绝大多数属性都是批量分配安全的。我只需要将:account_id和:is_admin等几个属性列入黑名单。是否可以将属性列入黑名单而不是将几乎所有属性列入白名单?例如:defuser_paramsparams.require(:user).exclude(:account_i

ruby-on-rails - 错误 : When assigning attributes, 您必须将散列作为参数传递

嗨,我刚开始使用ruby​​,我正在编写Controller和Controller规范,但我遇到了一些问题。文档.rbclassDocument文档Controller.rbclassAPI::DocumentsControllerdocuments_controller_spec.rbdescribe"POST'index'"dobefore{@attr=FactoryGirl.attributes_for(:document)}describe"failure"dodescribe"withmissingparameters"dobefore{@attr.each{|key,val

ruby-on-rails - Rails,activerecord : self[:attribute] vs self. 属性

在Rails中访问事件记录列/属性时,使用self[:attribute]与self.attribute有什么区别?这会影响getter和setter吗? 最佳答案 它们都只是获取属性的方法-它们都只是getter。self.attribtue是一个更“传统”的getter,而self[:attribute]基本上只是[]方法。在使用两者之间切换不会产生任何影响。我建议只使用self.attribute方法,因为它在语法上更好。但是,当其他内容覆盖self.attribute方法时,使用self[:attribute]会派上用场。例

ruby-on-rails - Elasticsearch /轮胎 : How do I filter a boolean attribute?

我想过滤我类(class)的私有(private)bool值,以便它只显示非私有(private)但对我不起作用的资源。(我大大简化了代码)mappingdoindexes:private,type:"boolean"indexes:name,type:"string"endenddefself.search(params)tire.search(load:true,page:params[:page],per_page:20)doquery{stringparams[:query]}ifparams[:query].present?#SofarI'vetried...#filter:

ruby-on-rails - 尝试使用 accepts_nested_attributes_for 和 has_and_belongs_to_many 但未填充连接表

我正在学习RoR并尝试使用accepts_nested_attributes_for和has_and_belongs_to_many来提交传统上为两种形式的信息。我在一些网站上看到它们兼容,一些网站不兼容,还有一些网站不知道。作为引用,我使用的是Rails2.3.4。我尝试根据nestedmodels上的Ryan'sScraps教程对我的解决方案进行建模从我尝试调试的内容来看,我似乎有两个问题,但我不确定为什么。当我提交包含嵌套模型的表单时,只发布了部分嵌套模型信息。我只得到第一个字段,而不是用户可能选择的“n”个其他字段在发布的单个字段中,没有任何行插入到我为HABTM关系创建的连接

ruby - 数组的 attr_accessor?

我想使用attr_accessor将数组作为实例变量。但是attr_accessor不只是用于字符串吗?如何在阵列上使用它?更新:例如。如果你想:object.array="cat"object.array="dog"ppobject.array=>["cat","dog"]那么你必须自己创建这些方法吗? 最佳答案 classSomeObjectattr_accessor:arraydefinitializeself.array=[]endendo=SomeObject.newo.array.push:ao.array.push:b