Rails has_many :through Find by Extra Attributes in Join Model
How about adding something like this into your User model?
has_many :active_events, :through => :event_users,
:class_name => "Event",
:source => :event,
:conditions => ['event_users.active = ?',true]
After that you should be able to get active events for a user just by calling:
User.first.active_events
Add an attribute to a joining model in Rails has_many through
I think you should create a separate model for this Recommendation, dependent of the Like model:
class Book < ActiveRecord::Base
belongs_to :user
has_many :comments
has_many :commenters, :through => :comments, :source => :user
class User < ActiveRecord::Base
has_many :books
has_many :comments
has_many :commented_books, :through => :comments, :source => :book
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :book
Then, the logic to create a comment for a book:
# Books Controller
def show
@book = Book.find(params[:id])
@comment = @book.comments.build
end
# show view of the book
form_for @comment do |form|
form.hidden_field :user_id, value: current_user.id
form.hidden_field :book_id
form.text_area :content # the name of the attribute for the content of the comment
form.submit "Post comment!"
end
To list all the comments of a specific User:
# users controller (profile page)
def show
@user = User.find(params[:id])
end
# show view of Users
@user.comments.includes(:book).each do |comment|
"Comment on the book '#{comment.book.name}' :"
comment.content
end
using join model attributes in rails has_many :through
solved with
class Group
has_many :groups_users
has_many :moderatorships, :class_name => "GroupsUser",:conditions => {:moderator => true}
has_many :moderators, :through => :moderatorships, :class_name => "User", :source => :user
end
Preferred to Matt van Horn's answer because this one produces only one query when we select user info with @group.moderators, while his solution gives separate query for each moderator
edit: updated to answer Sizzlepants' question. moderators created with this code should have moderator attribute in join model set to true (rails uses :conditions while creating join model), also, if i'd have to code it now, i'd name groups_users memberships for readability.
how to access rails join model attributes when using has_many :through
In your example you have defined in Item model relationship as has_many for collection_items and collections the generated association method is collection_items and collections respectively both of them returns an array so the way you are trying to access here is wrong. this is primarily case of mant to many relationship. just check this Asscociation Documentation for further reference.
Related Topics
How to Check If a Value Exists in an Array in Ruby
Understanding the Rails Authenticity Token
How to Find Where a Method Is Defined At Runtime
Sudo Gem Install' or 'Gem Install' and Gem Locations
How to Skip the First Line of a CSV File and Make the Second Line the Header
Ssl Error When Installing Rubygems, Unable to Pull Data from 'Https://Rubygems.Org/
How to Sort an Array in Descending Order in Ruby
Rails 4: List of Available Datatypes
Validation for Non-Negative Integers and Decimal Values
Does Ruby Have Real Multithreading
Combining Implicit Wait and Explicit Wait Together Results in Unexpected Wait Times
What Does the "===" Operator Do in Ruby
How to Calculate Number of Chars Common to Two Strings
The Authorization Mechanism You Have Provided Is Not Supported. Please Use Aws4-Hmac-Sha256
How to Convert a String Object into a Hash Object
Cannot Load Such File - Zlib Even After Using Rvm Pkg Install Zlib