In my controller, I can get the parameters I want out of the params hash like so: =======user = @params["user"] email = user["email"] password = user["password"] ======= Simple enough. Now, assuming there are more fields in my User object I want populated by default, I could do something like so: =======user = User.new user.attributes = @params["user"] password = user.[]("password") ======= Is this a correct assumption ? Just want to make sure there isn''t an easier way I may be overlooking. Thanks ! _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Nov 1, 2005, at 9:16 PM, Dylan Stamat wrote:> In my controller, I can get the parameters I want out of the params > hash like so: > =======> user = @params["user"] > email = user["email"] > password = user["password"] > =======> > Simple enough. Now, assuming there are more fields in my User > object I want populated by default, I could do something like so: > =======> user = User.new > user.attributes = @params["user"] > password = user.[]("password") > =======> > Is this a correct assumption ? Just want to make sure there isn''t > an easier way I may be overlooking. > Thanks !user = User.new(@params["user"]) user.password I recommend Dave Thomas'' Rails book; it includes a good primer on Ruby as well. jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDaFaiAQHALep9HFYRAmWiAJ91L/yd7s8SMIHSUsfuWaD24obacgCdGJMs K+JfCu/1lruD3Zl2QhW415g=4K3i -----END PGP SIGNATURE-----
ahh... thanks Jeremy. I actually did buy both books from the "Pragmatic Programmers"... but obviously missed that :) Thanks a bunch ! On 11/1/05, Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> wrote:> > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On Nov 1, 2005, at 9:16 PM, Dylan Stamat wrote: > > In my controller, I can get the parameters I want out of the params > > hash like so: > > =======> > user = @params["user"] > > email = user["email"] > > password = user["password"] > > =======> > > > Simple enough. Now, assuming there are more fields in my User > > object I want populated by default, I could do something like so: > > =======> > user = User.new > > user.attributes = @params["user"] > > password = user.[]("password") > > =======> > > > Is this a correct assumption ? Just want to make sure there isn''t > > an easier way I may be overlooking. > > Thanks ! > > user = User.new(@params["user"]) > user.password > > I recommend Dave Thomas'' Rails book; it includes a good primer on > Ruby as well. > > jeremy > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.2 (Darwin) > > iD8DBQFDaFaiAQHALep9HFYRAmWiAJ91L/yd7s8SMIHSUsfuWaD24obacgCdGJMs > K+JfCu/1lruD3Zl2QhW415g> =4K3i > -----END PGP SIGNATURE----- > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I just did some tests, and it looks as if your method works perfectly... assuming the "User" class doesn''t have any "attr_reader" attributes specified. I had the following in my "User" class, which was causing and subsequent user.email to spit out nil: attr_reader :email Removing the attr_reader from the "User" class fixed the problem. Any reason to this ? On 11/2/05, Dylan Stamat <dylans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > ahh... thanks Jeremy. I actually did buy both books from the "Pragmatic > Programmers"... but obviously missed that :) Thanks a bunch ! > > > On 11/1/05, Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> wrote: > > > > -----BEGIN PGP SIGNED MESSAGE----- > > Hash: SHA1 > > > > On Nov 1, 2005, at 9:16 PM, Dylan Stamat wrote: > > > In my controller, I can get the parameters I want out of the params > > > hash like so: > > > =======> > > user = @params["user"] > > > email = user["email"] > > > password = user["password"] > > > =======> > > > > > Simple enough. Now, assuming there are more fields in my User > > > object I want populated by default, I could do something like so: > > > =======> > > user = User.new > > > user.attributes = @params["user"] > > > password = user.[]("password") > > > =======> > > > > > Is this a correct assumption ? Just want to make sure there isn''t > > > an easier way I may be overlooking. > > > Thanks ! > > > > user = User.new(@params["user"]) > > user.password > > > > I recommend Dave Thomas'' Rails book; it includes a good primer on > > Ruby as well. > > > > jeremy > > -----BEGIN PGP SIGNATURE----- > > Version: GnuPG v1.4.2 (Darwin) > > > > iD8DBQFDaFaiAQHALep9HFYRAmWiAJ91L/yd7s8SMIHSUsfuWaD24obacgCdGJMs > > K+JfCu/1lruD3Zl2QhW415g> > =4K3i > > -----END PGP SIGNATURE----- > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
The following is my understanding... attr_read :email is the same as writing: def email @email end Which bypasses ActiveRecords introspection to get the email value. ARs introspection would do something more like: def email attributes["email"] end where attributes is the hash that stores the ActiveRecord fields (I''m not sure it quite like that). Regards, Nick On 11/2/05, Dylan Stamat <dylans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I just did some tests, and it looks as if your method works perfectly... > assuming the "User" class doesn''t have any "attr_reader" attributes > specified. I had the following in my "User" class, which was causing and > subsequent user.email to spit out nil: > > attr_reader :email > > Removing the attr_reader from the "User" class fixed the problem. Any > reason to this ? > > > > On 11/2/05, Dylan Stamat <dylans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > ahh... thanks Jeremy. I actually did buy both books from the "Pragmatic > Programmers"... but obviously missed that :) Thanks a bunch ! > > > > > > > > > > On 11/1/05, Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org > wrote: > > > -----BEGIN PGP SIGNED MESSAGE----- > > > Hash: SHA1 > > > > > > On Nov 1, 2005, at 9:16 PM, Dylan Stamat wrote: > > > > In my controller, I can get the parameters I want out of the params > > > > hash like so: > > > > =======> > > > user = @params["user"] > > > > email = user["email"] > > > > password = user["password"] > > > > =======> > > > > > > > Simple enough. Now, assuming there are more fields in my User > > > > object I want populated by default, I could do something like so: > > > > =======> > > > user = User.new > > > > user.attributes = @params["user"] > > > > password = user.[]("password") > > > > =======> > > > > > > > Is this a correct assumption ? Just want to make sure there isn''t > > > > an easier way I may be overlooking. > > > > Thanks ! > > > > > > user = User.new(@params["user"]) > > > user.password > > > > > > I recommend Dave Thomas'' Rails book; it includes a good primer on > > > Ruby as well. > > > > > > jeremy > > > -----BEGIN PGP SIGNATURE----- > > > Version: GnuPG v1.4.2 (Darwin) > > > > > > > iD8DBQFDaFaiAQHALep9HFYRAmWiAJ91L/yd7s8SMIHSUsfuWaD24obacgCdGJMs > > > K+JfCu/1lruD3Zl2QhW415g> > > =4K3i > > > -----END PGP SIGNATURE----- > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Nicholas Van Weerdenburg
That makes perfect sense, and that''s what definitely explain what''s happening. So, if I had another service that needed to get the email from my User object (a remote class for example), how would this be setup? If I omit the "attr_reader" clause, I''m now hiding the accessor methods for these variables... correct ? Thanks Nicholas ! On 11/2/05, Nicholas Van Weerdenburg <vanweerd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > The following is my understanding... > > attr_read :email > > is the same as writing: > > def email > @email > end > > Which bypasses ActiveRecords introspection to get the email value. ARs > introspection would do something more like: > > def email > attributes["email"] > end > > where attributes is the hash that stores the ActiveRecord fields (I''m > not sure it quite like that). > > Regards, > Nick > > On 11/2/05, Dylan Stamat <dylans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I just did some tests, and it looks as if your method works perfectly... > > assuming the "User" class doesn''t have any "attr_reader" attributes > > specified. I had the following in my "User" class, which was causing and > > subsequent user.email to spit out nil: > > > > attr_reader :email > > > > Removing the attr_reader from the "User" class fixed the problem. Any > > reason to this ? > > > > > > > > On 11/2/05, Dylan Stamat <dylans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > ahh... thanks Jeremy. I actually did buy both books from the > "Pragmatic > > Programmers"... but obviously missed that :) Thanks a bunch ! > > > > > > > > > > > > > > > On 11/1/05, Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org > wrote: > > > > -----BEGIN PGP SIGNED MESSAGE----- > > > > Hash: SHA1 > > > > > > > > On Nov 1, 2005, at 9:16 PM, Dylan Stamat wrote: > > > > > In my controller, I can get the parameters I want out of the > params > > > > > hash like so: > > > > > =======> > > > > user = @params["user"] > > > > > email = user["email"] > > > > > password = user["password"] > > > > > =======> > > > > > > > > > Simple enough. Now, assuming there are more fields in my User > > > > > object I want populated by default, I could do something like so: > > > > > =======> > > > > user = User.new > > > > > user.attributes = @params["user"] > > > > > password = user.[]("password") > > > > > =======> > > > > > > > > > Is this a correct assumption ? Just want to make sure there isn''t > > > > > an easier way I may be overlooking. > > > > > Thanks ! > > > > > > > > user = User.new(@params["user"]) > > > > user.password > > > > > > > > I recommend Dave Thomas'' Rails book; it includes a good primer on > > > > Ruby as well. > > > > > > > > jeremy > > > > -----BEGIN PGP SIGNATURE----- > > > > Version: GnuPG v1.4.2 (Darwin) > > > > > > > > > > iD8DBQFDaFaiAQHALep9HFYRAmWiAJ91L/yd7s8SMIHSUsfuWaD24obacgCdGJMs > > > > K+JfCu/1lruD3Zl2QhW415g> > > > =4K3i > > > > -----END PGP SIGNATURE----- > > > > _______________________________________________ > > > > Rails mailing list > > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > -- > Nicholas Van Weerdenburg > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
ActiveRecord uses introspection via method_missing to handle this for you. record.email will work fine as a result. On 11/2/05, Dylan Stamat <dylans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> That makes perfect sense, and that''s what definitely explain what''s > happening. > > So, if I had another service that needed to get the email from my User > object (a remote class for example), how would this be setup? > If I omit the "attr_reader" clause, I''m now hiding the accessor methods for > these variables... correct ? > > Thanks Nicholas ! > > > > On 11/2/05, Nicholas Van Weerdenburg <vanweerd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > The following is my understanding... > > > > attr_read :email > > > > is the same as writing: > > > > def email > > @email > > end > > > > Which bypasses ActiveRecords introspection to get the email value. ARs > > introspection would do something more like: > > > > def email > > attributes["email"] > > end > > > > where attributes is the hash that stores the ActiveRecord fields (I''m > > not sure it quite like that). > > > > Regards, > > Nick > > > > On 11/2/05, Dylan Stamat < dylans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I just did some tests, and it looks as if your method works perfectly... > > > assuming the "User" class doesn''t have any "attr_reader" attributes > > > specified. I had the following in my "User" class, which was causing > and > > > subsequent user.email to spit out nil: > > > > > > attr_reader :email > > > > > > Removing the attr_reader from the "User" class fixed the problem. Any > > > reason to this ? > > > > > > > > > > > > On 11/2/05, Dylan Stamat <dylans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > ahh... thanks Jeremy. I actually did buy both books from the > "Pragmatic > > > Programmers"... but obviously missed that :) Thanks a bunch ! > > > > > > > > > > > > > > > > > > > > On 11/1/05, Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org > wrote: > > > > > -----BEGIN PGP SIGNED MESSAGE----- > > > > > Hash: SHA1 > > > > > > > > > > On Nov 1, 2005, at 9:16 PM, Dylan Stamat wrote: > > > > > > In my controller, I can get the parameters I want out of the > params > > > > > > hash like so: > > > > > > =======> > > > > > user = @params["user"] > > > > > > email = user["email"] > > > > > > password = user["password"] > > > > > > =======> > > > > > > > > > > > Simple enough. Now, assuming there are more fields in my User > > > > > > object I want populated by default, I could do something like so: > > > > > > =======> > > > > > user = User.new > > > > > > user.attributes = @params["user"] > > > > > > password = user.[]("password") > > > > > > =======> > > > > > > > > > > > Is this a correct assumption ? Just want to make sure there isn''t > > > > > > an easier way I may be overlooking. > > > > > > Thanks ! > > > > > > > > > > user = User.new(@params["user"]) > > > > > user.password > > > > > > > > > > I recommend Dave Thomas'' Rails book; it includes a good primer on > > > > > Ruby as well. > > > > > > > > > > jeremy > > > > > -----BEGIN PGP SIGNATURE----- > > > > > Version: GnuPG v1.4.2 (Darwin) > > > > > > > > > > > > > > iD8DBQFDaFaiAQHALep9HFYRAmWiAJ91L/yd7s8SMIHSUsfuWaD24obacgCdGJMs > > > > > K+JfCu/1lruD3Zl2QhW415g> > > > > =4K3i > > > > > -----END PGP SIGNATURE----- > > > > > _______________________________________________ > > > > > Rails mailing list > > > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > > > > > > > > > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > > > > -- > > Nicholas Van Weerdenburg > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Nicholas Van Weerdenburg