English 中文(简体)
方法价值上的归属
原标题:set attribute from value of method

我确信,我只是这样做,有人能否向我指出正确的方向?

我需要确定有效日期至修改格式日期的价值。

www.un.org/Depts/DGACM/index_french.htm

class Request < ActiveRecord::Base

  def format_date=(format_date)
    date_a = format_date.split("/")
    month, day, year = date_a
    effective_date = Date.parse("#{year}-#{month}-#{day}")
  end

  def format_date
    effective_date
  end
end

Request.create (format_date: “04/21/2012”> is not establishing the Value to effect_date

Edit:我猜测这真的意思,因此,我将尽力解释:

我需要从格式(而不是数据库中的一栏)中确定有效日期的价值。 我正在使用格式_日期方法,将日期从格式_日期改为生效日期。 如果这只字不提,让我知道。 我不相信还有什么。

最佳回答

如果<代码>有效_date是贵国数据库中的一栏名称,该改动就简单明了。

def format_date=(format_date)
  date_a = format_date.split("/")
  month, day, year = date_a
  self.effective_date = Date.parse("#{year}-#{month}-#{day}")
end

唯一的改动是<代码> 有效_date = to Self. Effective_date =。 无需添加<条码>。 在那里,口译人员没有办法知道你是否希望确定一个称为<代码>有效_date的地方变量。 或称<代码> 有效_date=方法。 它假定当地变量。

问题回答

如果“条形形形形形形形形色色”是属性的,但不是在表格中,那么我就认为需要改动。

class Request < AR::Base
  ...
  def format_date=(f_date)
    ...
    @effective_date = Date.parse("#{year}-#{month}-#{day}")
  end

  def format_date
    @effective_date
  end
end

此外,用名称作为方法名称(日期)使用当地变数并不好,但这不会产生错误,而是产生误导。

UPDATE: You can see more aboute original characteristics here:

http://railscasts.com/episodes/167-more-on-virtual-attributes” rel=“nofollow”http://railscasts.com/episodes/167-more-on-virtual-attributes





相关问题
Remove ActiveRecord in Rails 3

Now that Rails 3 beta is out, I thought I d have a look at rewriting an app I have just started work on in Rails 3 beta, both to get a feel for it and get a bit of a head-start. The app uses MongoDB ...

When will you upgrade your app to Rails 3? [closed]

Now that the Rails 3 beta is here, let s take a little straw poll. Please tell us briefly what your application does and when you will upgrade it to Rails 3. Or, if you re not planning on upgrading ...

Bundler isn t loading gems

I have been having a problem with using Bundler and being able to access my gems without having to require them somewhere, as config.gem used to do that for me (as far as I know). In my Rails 3 app, I ...

bypass attr_accessible/protected in rails

I have a model that, when it instantiates an object, also creates another object with the same user id. class Foo > ActiveRecord::Base after_create: create_bar private def create_bar Bar....

concat two fields activerecord

I m so used to oracle where you can simply concat(field1, , field2) but if I m using activerecord to find the field1 and field2, and I need a space in between, how do I accomplish this? Cheers ...

热门标签