has-many-through
Rails 4 Form: has_many through: checkboxes
collection_check_boxes is a better option: <%= f.collection_check_boxes(:animal_ids, Animal.all, :id, :name) do |animal| %> <%= animal.label { animal.check_box } %> <% end %>
how to avoid duplicates in a has_many :through relationship?
Simpler solution that’s built into Rails: class Blog < ActiveRecord::Base has_many :blogs_readers, :dependent => :destroy has_many :readers, :through => :blogs_readers, :uniq => true end class Reader < ActiveRecord::Base has_many :blogs_readers, :dependent => :destroy has_many :blogs, :through => :blogs_readers, :uniq => true end class BlogsReaders < ActiveRecord::Base belongs_to :blog belongs_to :reader end Note adding the :uniq … Read more
Rails: HasManyThroughAssociationNotFoundError
You need to add has_many :entries To each of your models, since the :through option just specifies a second association which it should use to find the other side.
Rails Associations – has_many => :through – but same model
That was an interesting question. I just created a working app for your use case. post.related_posts will give you all posts related from post, while post.inverse_related_posts will give you all posts related to post. Here’s what my models look like: class Post < ActiveRecord::Base has_many :related_posts_association, :class_name => “RelatedPost” has_many :related_posts, :through => :related_posts_association, :source … Read more
Validate that an object has one or more associated objects
There is a validation that will check the length of your association. Try this: class Product < ActiveRecord::Base has_many :product_categories has_many :categories, :through => :product_categories validates :categories, :length => { :minimum => 1 } end
Validate that an object has one or more associated objects
There is a validation that will check the length of your association. Try this: class Product < ActiveRecord::Base has_many :product_categories has_many :categories, :through => :product_categories validates :categories, :length => { :minimum => 1 } end
Active Record has_many :through remove one associated record
here’s what you want: photo.tags.delete(tag)
Rails: has_many through with polymorphic association – will this work?
You have to do: class Person < ActiveRecord::Base has_many :events has_many :meals, :through => :events, :source => :eventable, :source_type => “Meal” has_many :workouts, :through => :events, :source => :eventable, :source_type => “Workout” end This will enable you to do this: p = Person.find(1) # get a person’s meals p.meals.each do |m| puts m end # … Read more
dependent => destroy on a “has_many through” association
Apparently :dependent is not ignored! The real issue was that I was calling Comment.delete(id) which goes straight to the db, whereas I now use Comment.destroy(id) which loads the Comment object and calls destroy() on it. This picks up the :dependent => :destroy and all is well.