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


On Fri, Aug 15, 2014 at 11:57 AM, Turcu Marius <li...@ruby-forum.com> wrote:

> 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.
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-talk+unsubscr...@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/ed67b2d2cb8937bf30ffd1595db591b6%40ruby-forum.com
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CANBna%3DDKDm%2BSbKFAxwmJM%2ByE4DSxWh5pWpBqgnqELW60cwsorA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to