Tuesday, 15 September 2015

Rails throwing error: Association named 'media' was not found on User; perhaps you misspelled it? -



Rails throwing error: Association named 'media' was not found on User; perhaps you misspelled it? -

so trying find users have made comment under same picture.

in psql terminal write , gets me info need:

select * users inner bring together comments on users.id = comments.user_id inner bring together media on media.id = comments.media_id media_id = 1

now in rails doing this:

@users = user.joins(:comments, :media).where(media_id = @medium.id)

which should work think. when trying print out user names @users in view, error thrown:

association named 'media' not found on user; perhaps misspelled it?

have done bring together clause wrong here?

db schema:

create_table "media", force: true |t| # unrelevant columns t.integer "user_id" end add_index "media", ["user_id"], name: "index_media_on_user_id", using: :btree create_table "comments", force: true |t| # unrelevant columns t.integer "user_id", null: false t.integer "media_id", null: false end add_index "comments", ["media_id"], name: "index_posts_on_media_id", using: :btree add_index "comments", ["user_id"], name: "index_posts_on_user_id", using: :btree create_table "users", force: true |t| t.string "name", default: "", null: false end

since media associated straight comments instead of users, should have:

@users = user.joins(comments: :media).where(comments: {media_id: @medium.id})

ruby-on-rails

No comments:

Post a Comment