1

I'm creating a sample_app with railstutorial.org and I have a small problem with creating a new User in Rails 4. I'm in a chapter 6: http://ruby.railstutorial.org/chapters/modeling-users#sec-the_model_file

After creating a new user in Rails console for example:

 user = User.new(name: "Example", email: "email@example.com")

I got:

 #<User id: 2, name: nil, email: nil, created_at: "2013-12-29 19:53:25", updated_at: "2013-12-29 19:53:25"> 

So that's mean, that I've just created a new user with nil params, but I initialized a name and email....

but when I type:

user.name

I got:

user.name
=> "Example" 

also in sqlite3 data browser application I see only an empty columns.

My user.rb looks like:

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}
                #:uniqueness => true
      validates :email, :presence   => true,
                :format     => {with: email_regex},
                :uniqueness => {:case_sensitive => false}
end

So what's wrong? Any idea how to fix that?

Neologis
  • 13
  • 3
  • You must `save` after `new`, so after `user = User.new(name: "Example", email: "email@example.com")` type `user.save` to save it to the database. – Vucko Dec 29 '13 at 20:08
  • It doesn't work, it works same with a save like as w/o save. – Neologis Dec 29 '13 at 20:11
  • I agree with @Vucko. After creating the new user you need to `user.save`. If that still isn't working then maybe try to see if there were any errors. Try `user.errors` and see if there were any errors when saving. – covard Dec 29 '13 at 20:22
  • 1
    I saw that, when I modified attr_accessor to attr_writter now it shows a user properly. So what is the difference between responding to attr_accesor and attr_writter. I know that attr_accessor = attr_reader + attr_writter, but why one method shows a user and the other not? – Neologis Dec 29 '13 at 20:34
  • Not a critical answer but may be a helpful one: http://stackoverflow.com/a/4701350/2930161 Related: http://stackoverflow.com/q/11521249/2930161 http://stackoverflow.com/q/7081477/2930161 – tkymtk Dec 30 '13 at 04:40

2 Answers2

0

Remove the attr_accessor line altogether, the Rails tutorial doesn't use it, and you don't need it.

Problem solved :)

As for why that doesn't work with accessor but it does with writer, I don't know, but you can definitely do the tutorial for now without getting stuck on that detail.

Tom
  • 330
  • 4
  • 15
0

You are confusing attr_accessor with attr_accessible.

attr_accessor is used to create setter and getter methods, which is why you are able to retrieve the results from user.name.

attr_accessible is used to white-list attributes for mass-assignment, essentially allowing you to pass those parameters up through the ORM.

In your case, the creation of these getter and setter methods are overriding the default methods that ActiveRecord::Base has already created for you. Use of attr_accessor would be for a variable that is not already defined by the columns in your table.

class User < ActiveRecord::Base
     attr_accessible :name, :email

     email_regex = /\A[\w.\-]+@[a-z+\d\-.]+\.+[a-z]+\z/i

      validates :name,  :presence   => true,
                :length     => {maximum: 50}
                #:uniqueness => true
      validates :email, :presence   => true,
                :format     => {with: email_regex},
                :uniqueness => {:case_sensitive => false}
end
saneshark
  • 1,243
  • 13
  • 25