I have 3 tables, one called events, one called users, and one called
created_events which contains an event id and a user id (in order to
link a user and an event together) - each one corresponds to its
relevent model.
I would like to run a query to return the created event for a certain
user by passing their ID to the created_events table - and then return
the relevent rows, plus the event data (linked by the event id).
Is there a way to do this in rails - I could do it in SQL using joins
but am a little unsure of the rails approach!
I have 3 tables, one called events, one called users, and one called
created_events which contains an event id and a user id (in order to
link a user and an event together) - each one corresponds to its
relevent model.
I would like to run a query to return the created event for a certain
user by passing their ID to the created_events table - and then return
the relevent rows, plus the event data (linked by the event id).
Is there a way to do this in rails - I could do it in SQL using joins
but am a little unsure of the rails approach!
FYI this is how I have done it in SQL:
@created_events = CreatedEvent.find_by_sql("SELECT ce., e. from
created_events ce, events e WHERE (e.id = ce.event_id) AND ce.user_id =
" + params[:id])
Hi - once I have done all that, should calling “event.users” return all
the users associated with an event (as defined in the events_users
table) as currently I get the error “undefined method `events’ for
EventUser:Class” - I am very new to rails by the way so Im sure this is
a simple error on my part!!
OK, first sorry for the double post!
I have revised my model thus: I have a link table which stores a user id
and an event id, plus two extra columns “subscribed” and “created” -
users can create and subscribe to events independently.
class User < ActiveRecord::Base
belongs_to :user_events
has_many :events, :through => :user_events
class UserEvent < ActiveRecord::Base
has_many :events
has_many :users
with database tables “events”, “users” and “user_events” but I am
currently getting the error “uninitialized constant User::UserEvents”
from this call “User.find(params[:id]).events”. Is this poor
pluralisation on my part?
Any help is appreciated!
Cheers Mike
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.