I have following associations:
class ConferenceSession < ActiveRecord::Base
has_many :conference_sessions
end
class ConferenceSession < ActiveRecord::Base
belongs_to :conference
has_and_belongs_to_many :users
end
class User < ActiveRecord::Base
has_and_belongs_to_many :conference_sessions
end
and a conference_sesssions_users
table. Admin can allow specific people to attend a particular session. So I want to form a query that whenever a user logs in and selects a conference he should be allowed to see only those sessions where the admin has allowed him to. I tried this:
scope :visibility,
lambda { |user_id| joins(:conference_sessions_users).where('conference_sessions_users.user_id = ?' => user_id) }
but I'm getting an error ActiveRecord::ConfigurationError: Association named 'conference_sessions_users' was not found on ConferenceSession; perhaps you misspelled it?
ActiveRecord::ConfigurationError: Association named 'conference_sessions_users' was not found on ConferenceSession; perhaps you misspelled it?
What could be wrong?
The error is referring to the joins(:conference_sessions_users)
part of your scope. For the joins
argument "The given symbol(s) should match the name of the association(s)."
You've used the name of the join table which isn't right in this situation.
To use the association you'd need: joins(:user)
If you do just want to use the joining table you can use a string to define the join:
joins("inner join conference_sessions_users
on conference_session_id = conference_sessions.id")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.