arrays - Rails: PG::SyntaxError: ERROR: syntax error at or near ":" -
i'm trying write query:
@myquery = event.where("reservations.map(&:first)= ?", current_user.id)
but error:
actionview::template::error (pg::syntaxerror: error: syntax error @ or near ":" line 1: ...ect "trips".* "events" (reservations.map(&:first)= 1... ^
the goal of query find user ids in first element of array of arrays like: [["2","1"],["4","1"]] 2, 4 user ids. tried different solutions still have problem!
a little confused i'm guessing you're trying create many-to-many relationship between users
, events
(i.e. user
has many events
, event
has many users
).
if not want storing user_ids in event
model , vice versa. want to create join table holds ids each
#database table create_table "user_events", force: :cascade |t| t.integer "user_id" t.integer "event_id" t.datetime "created_at" t.datetime "updated_at" end #join table class userevents < activerecord::base belongs_to :user belongs_to :event end #user class user < activerecored::base has_many :user_events, dependent: :destroy has_many :events, through: :user_events end #event class event < activerecord::base has_many :user_events, dependent: :destroy has_many :users, through: :user_events end
with structure telling application users
, events
related through userevents
, have access sorts of useful keyword.
for example do
event.includes(:users).all.map {|event| event.user.first.id}
which return id of first user every event, think you're trying accomplish.
Comments
Post a Comment