On 19 April 2015 at 21:33, Arumoy Shome <[email protected]> wrote:
> I have run into a rather unusual situation and I can't seem to figure out
> why this is happening:
>
> I have a User model:
>
> class User < ActiveRecord::Base
>   has_many :posts, dependent: :destroy
> end
>
> And a Post model:
>
> class Post < ActiveRecord::Base
>   attr_accessor :content

That should either be attr_accessible or for rails 4 use strong parameters.

Colin

>   belongs_to :user
>   default_scope{order('created_at DESC')}
>
>   #validations
>   validates(:content, presence: true, length: {maximum: 140})
>   validates(:user_id, presence: true)
> end
>
> My migration for posts looks like this:
>
> require_relative '20150405091935_create_posts'
> class FixPosts < ActiveRecord::Migration
>   def change
>     revert CreatePosts #this was the original migration w/o a user reference
>
>     create_table :posts do |t|
>       t.belongs_to :user, index: true
>       t.string :content
>       t.integer :user_id
>
>       t.timestamps null: false
>     end
>   end
> end
>
> The schema generated seems to be in order
> post validations are in place and work fine (my specs are passing and I have
> done some manual testing in the console)
>
> The issue
>
> I create a post
>
> user = User.first
> user.posts.create(content: "This is a post.")
> => true
>
> however when I print it out I get the following:
>
> #<Post:0x007fc1a0f1d628
>   id: 1,
>   user_id: 1,
>   content: nil,
>   created_at: Sun, 19 Apr 2015 19:45:38 UTC +00:00,
>   updated_at: Sun, 19 Apr 2015 19:45:38 UTC +00:00>]
>
> the content is lost and returns a nil.
>
> What is going on here? Am I missing something?
>
> Any help will be appreciated!
>
> Thank you for your time.
>
> Please let me know if any additional info. is required. You can also see the
> entire code base here:
>
> https://github.com/arumoy-shome/prilepins_strength_program
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/b989dcc3-dbad-4778-9785-24d0327dc00a%40googlegroups.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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLtdQsScgyvNmHTcgDZHURg1VgtT8AHHJedhBzbmEmq3Dg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to