Find records with more than one ActiveRecord HABTM Association?

I've found the answer: User. Find(:all, :joins => :events, :select => 'DISTINCT `users`. *') Basically, the users restricts the result set to just the users table, and the DISTINCT keyword makes sure each user is only returned once.

I've found the answer: User. Find(:all, :joins => :events, :select => 'DISTINCT `users`. *') Basically, the users.

* restricts the result set to just the users table, and the DISTINCT keyword makes sure each user is only returned once.

1 This does not solve the problem for a case where you want more than N results, and not just 1. Can anyone offer an AR way to do it, other than just raw SQL? – Asaf Bartov Jan 18 '09 at 23:44.

With Active Record associations, we can streamline these — and other — operations by declaratively telling Rails that there is a connection between the two models. To learn more about the different types of associations, read the next section of this guide. That’s followed by some tips and tricks for working with associations, and then by a complete reference to the methods and options for associations in Rails.

In Rails, an association is a connection between two Active Record models. Associations are implemented using macro-style calls, so that you can declaratively add features to your models. For example, by declaring that one model belongs_to another, you instruct Rails to maintain Primary Key–Foreign Key information between instances of the two models, and you also get a number of utility methods added to your model.

In the remainder of this guide, you’ll learn how to declare and use the various forms of associations. But first, a quick introduction to the situations where each association type is appropriate. A belongs_to association sets up a one-to-one connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model.

A has_one association also sets up a one-to-one connection with another model, but with somewhat different semantics (and consequences). This association indicates that each instance of a model contains or possesses one instance of another model. A has_many association indicates a one-to-many connection with another model.

You’ll often find this association on the “other side” of a belongs_to association. This association indicates that each instance of the model has zero or more instances of another model. A has_many :through association is often used to set up a many-to-many connection with another model.

This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding through a third model. For example, consider a medical practice where patients make appointments to see physicians. The collection of join models can be managed via the API.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions