ruby on rails - What method will return ActiveRecord owner object from class method? -


let's have users have items:

class user < activerecord::base   has_many :items end 
class item < activerecord::base   belongs_to :user end 

now want add class method item customized record creation:

def self.create_personalized   create description: "#{user.name}' item" end 

but of course, since class method, user undefined. if call using user.items.create_personalized, there associated user via relationship. know item.create_personalized aware of user because works:

def self.create_personalized   item = create   item.update_attribute :description, "#{item.user.name}'s item" end 

but that's not best way access owner object. correct method?

if do

item.scope_attributes 

then you'll hash of attributes current scope apply item. these ones extracted scope if called create. these db level attributes you'll user id rather user itself.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -