Hi again. I''m trying to understand how to use relations inside Rails, but after a bunch of hours playing with my app I''m a little confused ... I have a table that contains my users and another one for the confirmation code and expiration date, with a foreign key (user_id). The confirmation "belongs_to" a user, and a user "has_one" confirmation, so everything is fine here, it''s simple to relation a model with another, wonderful job. My problem is the code, i.e. if the Confirmation table has a "code" attribute how do I access and set it from the User model ? Now I have this: def create_account(params) params[''enabled''] = ''0'' params[''registration''] = Date.today.to_s @user = User.new @user.attributes = params @user.confirmation[''expiration''] = Confirmation.calculate_expiration @user.confirmation[''code''] = Confirmation.create_confirmation_code @user end I''ve also tried @user.confirmation.attributes[''foo''] but frankly I don''t have a clue about what I''m doing :)) TIA, ngw -- checking for life_signs in -lKenny... no Oh my god, make (1) killed Kenny ! You, bastards ! nicholas_wieland-at-yahoo-dot-it
> I''ve also tried @user.confirmation.attributes[''foo''] but frankly I don''t > have a clue about what I''m doing :))Did you try @user.confirmation.foo= Somethingorother? Rails automatically creates accessors and mutators (getters and setters) for the properties of your object.> TIA, > ngw > > -- > checking for life_signs in -lKenny... no > Oh my god, make (1) killed Kenny ! You, bastards ! > > nicholas_wieland-at-yahoo-dot-it > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz
- Michael Koziarski :> > I''ve also tried @user.confirmation.attributes[''foo''] but frankly I don''t > > have a clue about what I''m doing :)) > > Did you try @user.confirmation.foo= Somethingorother? Rails > automatically creates accessors and mutators (getters and setters) for > the properties of your object.It doesn''t work, the mistake must be elsewhere. class User < ActiveRecord::Base has_one :confirmation def create_account(params) params[''enabled''] = ''0'' params[''registration''] = Date.today.to_s @user = User.new @user.attributes = params @user.confirmation.user_id = @user.id @user.confirmation.expiration = Confirmation.calculate_expiration @user.confirmation.code = Confirmation.create_confirmation_code @user end end class Confirmation < ActiveRecord::Base belongs_to :user def self.create_confirmation_code chars = (''a''..''z'').to_a + (1..9).to_a chars = chars.sort_by { rand } code = chars[0..10].to_s end def self.calculate_expiration expiration = (Date.today + 20).to_s end end The Confirmations table is really simple: user_id (FK), expiration (date), code (varchar). TIA, ngw -- checking for life_signs in -lKenny... no Oh my god, make (1) killed Kenny ! You, bastards ! nicholas_wieland-at-yahoo-dot-it
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Nicholas Wieland wrote:> class User < ActiveRecord::Base > has_one :confirmation > > def create_account(params) > params[''enabled''] = ''0'' > params[''registration''] = Date.today.to_s > @user = User.new > @user.attributes = params > @user.confirmation.user_id = @user.id > @user.confirmation.expiration = Confirmation.calculate_expiration > @user.confirmation.code = Confirmation.create_confirmation_code > @user > end > endYou must save the user before playing with its associations: transaction { user = User.create(params[''user'']) user.confirmation.create(params[''user''][''confirmation'']) } jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.6 (Darwin) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFB4XC5AQHALep9HFYRAolCAJ4j0eL2alhOzccdeOlxBGMJJlyQJACeKAjh YFc46StIq6Hjfk5Ph5uoCoA=VZxY -----END PGP SIGNATURE-----