model - Rails ActiveRecord insert a count field in record result -


hi :

class user < activerecord::base   attr_accessible :label, :description, :number    has_many :user_posts   has_many :posts, through: :user_posts    after_initialize :count    protected   def count     self.number = posts.count   end end 

i whant field :number count of posts of user (i think must defined in function count created in model. notice :number field not in db in model, want available in record returned user.last exemple.

i'm new activerecord maybe it's not way need go, i'm open sugestion.

try this:

class user < activerecord::base   attr_accessible :label, :description   attr_accessor :number    has_many :user_posts   has_many :posts, through: :user_posts    after_initialize :count    protected   def count     self.number = posts.count   end  end 

you don't need specify :number in attr_accessible list not taking value of number uer right?

for model attributes (not persistent) easiest way is, use attr_accessor


Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -