I''m a newbie learning Rails3 and this issue has my learning stopped in its tracks, so I''d greatly appreciate your help. I have a simple test app with a User model and name and email fields. In the console, if I run @test = User.create!(:name => ''Test Testman'', :email => ''test-J0of1frlU80@public.gmane.org'') I get => #<User id: 21, name: nil, email: nil, created_at: "2011-01-09 03:48:00", updated_at: "2011-01-09 03:48:00">. Where did my name and email data go? If I check the object I just created in the console, @test.name returns => "Test Testman" so it made it that far, and it also passed my model validation. Looking at the development log, I see NIL values for these fields in the SQL. Looking at the DB, I have rows being created with IDs and dates, but no name & email. How could data be present in the object but not get written into the SQL? My environment is running Ruby 1.9.2, Rails 3.0.3 and sqlite3-ruby 1.3.2 (I updated everything in an attempt to fix this) . Anyone ever see behavior like this and know the fix? Here''s all I''m trying to do: class User < ActiveRecord::Base attr_accessor :name, :email email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i validates :name, :presence => true, :length => { :maximum => 50 } validates :email, :presence => true, :format => { :with => email_regex }, :uniqueness => { :case_sensitive => false } end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 9, 4:20 am, Pixelguru <kencoxdes...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m a newbie learning Rails3 and this issue has my learning stopped in > its tracks, so I''d greatly appreciate your help. I have a simple test > app with a User model and name and email fields. In the console, if I > runDon''t use attr_accessor - you''re replacing the activerecord accessors that store data in the database with standard ruby ones (that don''t) Fred> @test = User.create!(:name => ''Test Testman'', :email => > ''t...-J0of1frlU80@public.gmane.org'') > I get > => #<User id: 21, name: nil, email: nil, created_at: "2011-01-09 > 03:48:00", updated_at: "2011-01-09 03:48:00">. > Where did my name and email data go? If I check the object I just > created in the console, > @test.name > returns > => "Test Testman" > so it made it that far, and it also passed my model validation. > Looking at the development log, I see NIL values for these fields in > the SQL. Looking at the DB, I have rows being created with IDs and > dates, but no name & email. How could data be present in the object > but not get written into the SQL? My environment is running Ruby > 1.9.2, Rails 3.0.3 and sqlite3-ruby 1.3.2 (I updated everything in an > attempt to fix this) . Anyone ever see behavior like this and know the > fix? > > Here''s all I''m trying to do: > > class User < ActiveRecord::Base > attr_accessor :name, :email > > email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i > > validates :name, > :presence => true, > :length => { :maximum => 50 } > > validates :email, > :presence => true, > :format => { :with => email_regex }, > :uniqueness => { :case_sensitive => false } > end-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thank you so much! The tutorial I was following had attr_accessible :name, :email and I had attr_accessor :name, :email I must have stared at it 100 times and not spotted the difference. On Jan 9, 7:26 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jan 9, 4:20 am, Pixelguru <kencoxdes...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I''m a newbie learning Rails3 and this issue has my learning stopped in > > its tracks, so I''d greatly appreciate your help. I have a simple test > > app with a User model and name and email fields. In the console, if I > > run > > Don''t use attr_accessor - you''re replacing the activerecord accessors > that store data in the database with standard ruby ones (that don''t) > > Fred > > > > > > > > > @test = User.create!(:name => ''Test Testman'', :email => > > ''t...-J0of1frlU80@public.gmane.org'') > > I get > > => #<User id: 21, name: nil, email: nil, created_at: "2011-01-09 > > 03:48:00", updated_at: "2011-01-09 03:48:00">. > > Where did my name and email data go? If I check the object I just > > created in the console, > > @test.name > > returns > > => "Test Testman" > > so it made it that far, and it also passed my model validation. > > Looking at the development log, I see NIL values for these fields in > > the SQL. Looking at the DB, I have rows being created with IDs and > > dates, but no name & email. How could data be present in the object > > but not get written into the SQL? My environment is running Ruby > > 1.9.2, Rails 3.0.3 and sqlite3-ruby 1.3.2 (I updated everything in an > > attempt to fix this) . Anyone ever see behavior like this and know the > > fix? > > > Here''s all I''m trying to do: > > > class User < ActiveRecord::Base > > attr_accessor :name, :email > > > email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i > > > validates :name, > > :presence => true, > > :length => { :maximum => 50 } > > > validates :email, > > :presence => true, > > :format => { :with => email_regex }, > > :uniqueness => { :case_sensitive => false } > > end-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.