ruby on rails - Has-Many and Belongs-to-one Relation -
ruby on rails - Has-Many and Belongs-to-one Relation -
i'm trying maintain track of how many signup_conversions user creates. therefore, have 2 next models:
signup_conversion.rbclass signupconversion < activerecord::base belongs_to :user belongs_to :convertee, :class_name => "user", :foreign_key => 'convertee_id' attr_accessible :convertee_id end
user.rb class user < activerecord::base attr_accessible :name, :email, :password, :password_confirmation belongs_to :signup_conversion has_many :signup_conversions end
would work way? or missing crucial here?
i haven't tried code, give tips hope find useful.
i think every has_many
/has_one
statement should have correspondent belongs_to
, 3 belongs_to
, 1 has_many
doesn't good.
i'm not sure has_one :signup_conversion
, has_many :signup_conversions
play together, i'd rather alter names. changed other names seek create associations clearer although i'm not sure understand real concepts represent. come improve names.
by default, foreign key guessed adding suffix _id
association name, don't need specify in case. also, don't think need create attribute accessible, @ to the lowest degree not association work.
signup_conversion.rb
class signupconversion < activerecord::base belongs_to :owner , :class_name => "user" belongs_to :convertee, :class_name => "user" end
user.rb
class user < activerecord::base attr_accessible :name, :email, :password, :password_confirmation has_one :owned_signup_conversion , :class_name => "signupconversion" has_many :triggered_signup_conversions, :class_name => "signupconversion" end
ruby-on-rails has-and-belongs-to-many
Comments
Post a Comment