简体   繁体   中英

Custom methods unacessible in Rails Model

When I try to:

class Construction < ActiveRecord::Base
  def columns
    ["a", "b"]
  end   
  store :dados, accessors: columns
end

I get:

undefined local variable or method `columns' for #<Class:0x007f891037dac0>

So, how should i do this?

PS: I have tried putting 'self.' before columns and it didn't work.

EDIT — More info about the problem:

I have set a series of Serialized Hash data stored on the column "dados". The method store does that and set attribute acessors. I have erased other parts of this code that are not really inherent to the problem, but basically, i need to inform the accesors attribute through a method instead of declaring directly there. The reason is because i'll reuse the method that generate the columns.

I wont be using the method in the instance variables, but instead inside the model itself. its for code reusing

when you create a method on a model, every instance of that model has its methods, for example if you do this:

In controller:

@construction=Construction.first

@construction.columns // will return that array;

can you give me more info on what you need to be done so i can help you better

mmm im still having trouble understanding the issue but maybe you should do an after create method, something like this:

class Construction < ActiveRecord::Base
  after_create :something

  def columns
    ["a", "b"]
  end   

  private
  def something
      store :dados, accessors: self.columns
  end
end

Ok. I solved it adding self. before, changed the columns to a different keyword cause its a rails method and things only worked after restarting the server. Thanks everyone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM