Forum: Ruby on Rails user -notes relationship

A551dd246cb624b7fd6a78d5ccee835d?d=identicon&s=25 Turcu Marius (mariusturcu)
on 2014-08-15 20:57
i have 2 models
class User < ActiveRecord::Base
  has_many :notes, :dependent => :destroy
  accepts_nested_attributes_for :notes
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

end


class Note < ActiveRecord::Base
  belongs_to :user
  validates :content, presence: true,length: { minimum: 15 }

end

the database schema

create_table "notes", primary_key: "note_id", force: true do |t|
    t.text    "content"
    t.integer "user_id"
  end



create_table "users", primary_key: "user_id", force: true do |t|
    t.string   "first_name"
    t.string   "last_name"
    t.string   "email",                  default: "",    null: false
    t.string   "encrypted_password",     default: "",    null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,     null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.boolean  "admin",                  default: false
  end

this is my method for saving notes
def save_note
    @note = Note.new(lesson_params)

    if @note.save
       flash[:notice] = "You have successfully add a lesson."
       redirect_to pages_home_url
    else
       flash[:notice] = "Failed."
       redirect_to pages_home_url
    end
  end


  private

  def lesson_params
    params.require(:note).permit(
      :content)
  end
end

when i save a note isn't put in my database the user_id and I don't know
why...I have to extract the curent user_id and i don't preaty much kknow
how to do it. Also, i check in console and this i get :" INSERT INTO
`notes` (`content`) VALUES ('asdasd asdakshdasmd ')". It seems like my
user_id isn't inserted in db. i came from another world called php and
there the things are different.
F099abfef46715b41b4d3d0e5764e697?d=identicon&s=25 Sean Kelley (Guest)
on 2014-08-15 22:21
(Received via mailing list)
I must be honest- I am running into this problem myself learning rails.
I
thought that when I create something the user_id should be generated
automatically in the created model.  While testing I figured I would
come
back to it.  In the meantime I set user_id in my entry table manually in
the create method ( I am using devise):
@entry = Entry.new(entry_params)
@entry.user_id=current_user[:id]
Dfc7587fd73f2efa19d6f1f9611b70ba?d=identicon&s=25 Jason Fb (jasonfb)
on 2014-08-15 22:30
(Received via mailing list)
On Aug 15, 2014, at 4:20 PM, Sean Kelley <kelleyfarm@gmail.com> wrote:

> I must be honest- I am running into this problem myself learning rails.  I
thought that when I create something the user_id should be generated 
automatically
in the created model.  While testing I figured I would come back to it.  In the
meantime I set user_id in my entry table manually in the create method ( I am
using devise):
> @entry = Entry.new(entry_params)
> @entry.user_id=current_user[:id]


see
http://guides.rubyonrails.org/association_basics.h...
Dfc7587fd73f2efa19d6f1f9611b70ba?d=identicon&s=25 Jason Fb (jasonfb)
on 2014-08-15 22:38
(Received via mailing list)
On Aug 15, 2014, at 2:57 PM, Turcu Marius <lists@ruby-forum.com> wrote:

> class Note < ActiveRecord::Base
>  end
>    t.datetime "remember_created_at"
> this is my method for saving notes
>  end
>


I assume the above code is in a CONTROLLER. If so, you should stick to
the controller actions index, new, create, edit, update, and show. Avoid
creating a controller action with another name.

You didn't show us the VIEW. In the VIEW, does the form you are
submitting contain a user_id ? If not, that is why you don't have a
user_id  in your notes record.

If this note is supposed to be associated to the currently logged in
user, you'll want to add

before_filter authenticate_user!

to the top of your controller, and also in your create method do
something like this:

current_user.notes.create(lesson_params)


You will find this documented in the section marked "has_many
Association Reference" on this page:

http://guides.rubyonrails.org/association_basics.h...

(By the way, the current_user method itself comes from devise, which
documentation you should thoroughly read here:
https://github.com/plataformatec/devise)

Finally, get yourself RubyMine and learn how to use the
Go-To-Declaration (Command-B). Do that NOW before you go any further.
(Another IDE is acceptable too if it has Go-To-Declaration
functionality)
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2014-08-15 22:55
(Received via mailing list)
On 15 August 2014 19:57, Turcu Marius <lists@ruby-forum.com> wrote:
> class Note < ActiveRecord::Base
>   belongs_to :user
>   validates :content, presence: true,length: { minimum: 15 }
>
> end
>
> the database schema
>
> create_table "notes", primary_key: "note_id", force: true do |t|

Why are you not using the default primary key (id)?  You will just
make life difficult if you avoid the rails conventions unless you have
a good reason for so doing.

Colin
9d3c2f3e230a6fbda25d4e53c1f05254?d=identicon&s=25 Mahcsig (Guest)
on 2014-08-16 00:06
(Received via mailing list)
If you update this:
@note = Note.new(lesson_params)

to:
@note = current_user.notes.new(lesson_params)

that should work the way you want.


~Mahcsig
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.