Tag: 属性

回调更改的ActiveRecord属性?

我知道ActiveRecord :: Dirty和相关的方法,但我没有看到我可以订阅属性更改事件的方法。 就像是: class Person < ActiveRecord::Base def attribute_changed(attribute_name, old_value, new_value) end #or attribute_changed do |attribute_name, old_value, new_value| end end 是否有Rails标准或插件? 我觉得它必定存在于某个地方,我只是想念它。

访问Tableless模型的id值时出现问题

我正在使用Ruby on Rails 3,我在Rails结构中遵循Tableless 模型 ,以便将其应用于我的模型帐户。 一切都有效,但如果我这样做的话 @test = account.id @test的调试导致零值,似乎根本无法访问。 在这个问题的评论中@Wukerplank说: You are right. I suppose id has a special status in ActiveRecord. I think it would only be set after the record is persisted in some database. 我怎样才能检索\访问id属性值? 更新 尝试并重新尝试我发现一个可能的解决方案是使所有属性’attr_accessible’(如果我只是’id’,我得到所有其他值’nil’),但我认为这是一个非常危险的解决方案。 另一个解决方案是创建一个充当id的新类属性,但是如果我已经拥有id ,为什么还要这样做?!

Rails模型:如何使属性受保护或私有在模型外不可见?

表格中有一些字段,我不希望在外面可见? 就像created_on,is_first等一样。我想通过在模型中使用回调来设置这些字段的值,但是某些人无法设置它。

Ruby OOP tic-tac-toe hash / conditional / itiration

试图在Ruby中创建一个经典的tic tac toe OOP但是我的game_results()方法遇到了麻烦。 我意识到这不是很完整,需要更多的function,但是现在我只是试图用我从用户输入的对象填充我的电路板并输出填充板和赢家。 当我在我的主板上看到我们有一个胜利者时,我呼吁使用game_results()方法并且它给了我正确的赢家,但是每当发生平局游戏时,我都会收到错误。 有人对操作员的错误有什么想法或解决方案吗? 顺便说一下,我知道这一切都很混乱,但我是初学者。 #require “pry” class Game attr_reader :turn def initialize @turn = 1 @board = { a1: ” “, a2: ” “, a3: ” “, b1: ” “, b2: ” “, b3: ” “, c1: ” “, c2: ” “, c3: ” ” } end # def start_game # […]

这个属性如何在ruby类中保存多个属性?

在这里,你看到我们有一个名为“属性”的属性,我们在我们的类中初始化它,所以问题是名称和衬衫属性来自哪里,因为我们不在我们的类中初始化和定义它们? class Shirt attr_accessor :attribute def initialize(attributes) @attributes = attributes end end store = Shirt.new(name: “go”, size: “42”) 当我检查这个衬衫类的实例时,我得到一个哈希: @attributes={:name=>”go”, :size=>”42″} 有人可以帮忙解释一下吗?

添加嵌套属性以设计用户模型

乍一看,这个问题似乎已经得到了回答,但不是我的情况(大部分都与质量分配问题和/或mongoid有关。)即使这里有很多有些相关的问题,我还是找不到答案。 。 所以基本上我在ruby on rails应用程序上使用devise gem进行用户身份validation,我想在用户模型中添加嵌套地址模型。 我也使用simple_form来生成表单视图。 我有一个用户模型: class User true end 这是地址模型: class Address < ActiveRecord::Base belongs_to :country belongs_to :state belongs_to :user attr_accessible :street1, :street2, :country_id, :state_id, :city, :postalCode end 这是用户控制器,我为了添加show动作来显示用户配置文件而进行了覆盖。 class Users::RegistrationsController after_sign_up_path_for(resource) else set_flash_message :notice, :”signed_up_but_#{resource.inactive_message}” if is_navigational_format? expire_session_data_after_sign_in! respond_with resource, :location => after_inactive_sign_up_path_for(resource) end else clean_up_passwords resource respond_with resource end end […]

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 […]

Rails没有保存更改的属性

我将一些文本附加到我的一个ActiveRecord::Base模型的notes字段中,但是当我保存它时,它不会更新: valve.notes #=> “Level: Top” valve.notes < “Level: Top\nDirection: North” valve.save #=> true valve.reload.notes #=> “Level: Top”

大厨11:任何方式将属性转换为ruby哈希?

我正在使用chef属性为我的服务生成配置。 但是,在某些时候,我需要将属性mash转换为简单的ruby哈希。 这曾经在Chef 10中正常工作: node.myapp.config.to_hash 但是,从Chef 11开始,这不起作用。 只有属性的顶级转换为散列,然后嵌套值保持不可变的mash对象。 修改它们会导致这样的错误: Chef :: Exceptions :: ImmutableAttributeModification ——————————————- —–如果未指定要设置的优先级,则节点属性是只读的。 要设置属性,请使用类似`node.default [“key”] =“value”’的代码 我已经尝试了很多方法来解决这个问题,但这些方法都不起作用: node.myapp.config.dup.to_hash JSON.parse(node.myapp.config.to_json) json解析hack,看起来应该很好用,导致: JSON::ParserError unexpected token at ‘”#”‘ 有没有任何实际可靠的方法,在每个cookbook中包含嵌套解析函数,将属性转换为简单,普通,好的旧ruby哈希?

如何初始化’attr_accessor’属性值?

可能重复: attr_accessor默认值 我正在使用Ruby on Rails 3.0.9,我想在我的class \ model中初始化一些inheritance自ActiveRecord::Base attr_accessor属性值。 那是, …在我的模块中我有: class User < ActiveRecord::Base attr_accessor :attribute_name1, :attribute_name2, :attribute_name3, … end 我想将所有attr_accessor属性值设置为true 。 我怎样才能做到这一点? PS:当然我想解决上述问题,接近“Ruby on Rails Way”。 我知道after_initialize回调但是通过使用该方法我应该重复每个attribute_name语句,我想在after_initialize语句中将值设置为true (…这不是 DRY – 不要重复自己)。 也许有更好的方法来实现这一目标。 当您声明这些属性时,有没有办法“动态”设置attr_accessor属性值? 也就是说,我希望立刻声明并设置attr_accessor属性!