Rails find record with zero has_many records associated [duplicate]
Bah, found it here: https://stackoverflow.com/a/5570221/417872 City.includes(:photos).where(photos: { city_id: nil })
Bah, found it here: https://stackoverflow.com/a/5570221/417872 City.includes(:photos).where(photos: { city_id: nil })
In this case the answer is pretty simple. Do not use many-to-many. Use pairing object. Exactly for the reasons you’ve mentioned: Extend the pairing object with more properties: Check here 24. Best Practices, a cite: Don’t use exotic association mappings. Good usecases for a real many-to-many associations are rare. Most of the time you need … Read more
Found a simple answer on IRC that seems to work (thanks to Radar): class Person < ActiveRecord::Base belongs_to :father, :class_name => ‘Person’ belongs_to :mother, :class_name => ‘Person’ has_many :children_of_father, :class_name => ‘Person’, :foreign_key => ‘father_id’ has_many :children_of_mother, :class_name => ‘Person’, :foreign_key => ‘mother_id’ def children children_of_mother + children_of_father end end
A better solution has been provided by @SooDesuNe on this SO post validates :tags, length: { minimum: 1, maximum: 6 }
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