# user -notes relationship

**URL:** <https://discuss.rubyonrails.org/t/user-notes-relationship/69463>\
**Category:** rubyonrails-talk\
**Created:** [August 15, 2014, 6:57pm UTC](https://discuss.rubyonrails.org/t/user-notes-relationship/69463 "2014-08-15T18:57:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [August 15, 2014, 6:57pm UTC](https://discuss.rubyonrails.org/t/user-notes-relationship/69463/1 "2014-08-15T18:57:25Z")

</div>

i have 2 models class User \< ActiveRecord::Base &nbsp;&nbsp;has\_many :notes, :dependent =\> :destroy &nbsp;&nbsp;accepts\_nested\_attributes\_for :notes &nbsp;&nbsp;devise :database\_authenticatable, :registerable, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:recoverable, :rememberable, :trackable, :validatable

end

class Note \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :user &nbsp;&nbsp;validates :content, presence: true,length: { minimum: 15 }

end

the database schema

create\_table "notes", primary\_key: "note\_id", force: true do |t| &nbsp;&nbsp;&nbsp;&nbsp;t.text "content" &nbsp;&nbsp;&nbsp;&nbsp;t.integer "user\_id" &nbsp;&nbsp;end

create\_table "users", primary\_key: "user\_id", force: true do |t| &nbsp;&nbsp;&nbsp;&nbsp;t.string "first\_name" &nbsp;&nbsp;&nbsp;&nbsp;t.string "last\_name" &nbsp;&nbsp;&nbsp;&nbsp;t.string "email", default: "", null: false &nbsp;&nbsp;&nbsp;&nbsp;t.string "encrypted\_password", default: "", null: false &nbsp;&nbsp;&nbsp;&nbsp;t.string "reset\_password\_token" &nbsp;&nbsp;&nbsp;&nbsp;t.datetime "reset\_password\_sent\_at" &nbsp;&nbsp;&nbsp;&nbsp;t.datetime "remember\_created\_at" &nbsp;&nbsp;&nbsp;&nbsp;t.integer "sign\_in\_count", default: 0, null: false &nbsp;&nbsp;&nbsp;&nbsp;t.datetime "current\_sign\_in\_at" &nbsp;&nbsp;&nbsp;&nbsp;t.datetime "last\_sign\_in\_at" &nbsp;&nbsp;&nbsp;&nbsp;t.string "current\_sign\_in\_ip" &nbsp;&nbsp;&nbsp;&nbsp;t.string "last\_sign\_in\_ip" &nbsp;&nbsp;&nbsp;&nbsp;t.datetime "created\_at" &nbsp;&nbsp;&nbsp;&nbsp;t.datetime "updated\_at" &nbsp;&nbsp;&nbsp;&nbsp;t.boolean "admin", default: false &nbsp;&nbsp;end

this is my method for saving notes def save\_note &nbsp;&nbsp;&nbsp;&nbsp;@note = Note.new(lesson\_params)

&nbsp;&nbsp;&nbsp;&nbsp;if @note.save &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = "You have successfully add a lesson." &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to pages\_home\_url &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = "Failed." &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to pages\_home\_url &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;private

&nbsp;&nbsp;def lesson\_params &nbsp;&nbsp;&nbsp;&nbsp;params.require(:note).permit( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:content) &nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Mahcsig](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@Mahcsig](https://discuss.rubyonrails.org/u/Mahcsig)\
**Post date:** [August 15, 2014, 8:05pm UTC](https://discuss.rubyonrails.org/t/user-notes-relationship/69463/2 "2014-08-15T20:05:23Z")

</div>

If you update this:

@note = Note.new(lesson\_params)

to:

@note = current\_user.notes.new(lesson\_params)

that should work the way you want.

---

<div class="post-metadata">

**Author:** ![Sean\_Kelley](https://avatars.discourse-cdn.com/v4/letter/s/858c86/32.png) [@Sean\_Kelley](https://discuss.rubyonrails.org/u/Sean_Kelley)\
**Post date:** [August 15, 2014, 8:20pm UTC](https://discuss.rubyonrails.org/t/user-notes-relationship/69463/3 "2014-08-15T20:20:38Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![Jason\_FB](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/jason_fb/32/826_2.png) [@Jason\_FB](https://discuss.rubyonrails.org/u/Jason_FB)\
**Post date:** [August 15, 2014, 8:29pm UTC](https://discuss.rubyonrails.org/t/user-notes-relationship/69463/4 "2014-08-15T20:29:15Z")

</div>

see [http://guides.rubyonrails.org/association\_basics.html#has-one-association-reference](http://guides.rubyonrails.org/association_basics.html#has-one-association-reference)

---

<div class="post-metadata">

**Author:** ![Jason\_FB](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/jason_fb/32/826_2.png) [@Jason\_FB](https://discuss.rubyonrails.org/u/Jason_FB)\
**Post date:** [August 15, 2014, 8:37pm UTC](https://discuss.rubyonrails.org/t/user-notes-relationship/69463/5 "2014-08-15T20:37:36Z")

</div>

> 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

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.html#has-many-association-reference](http://guides.rubyonrails.org/association_basics.html#has-many-association-reference)

(By the way, the current\_user method itself comes from devise, which documentation you should thoroughly read here: [https://github.com/plataformatec/devise](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)

---

<div class="post-metadata">

**Author:** ![Colin\_Law](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@Colin\_Law](https://discuss.rubyonrails.org/u/Colin_Law)\
**Post date:** [August 15, 2014, 8:53pm UTC](https://discuss.rubyonrails.org/t/user-notes-relationship/69463/6 "2014-08-15T20:53:57Z")

</div>

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
