Use reflect_on_all_associations class method on the User model This will return an array of reflection objects. On those objects you can call macro method to find out the association type ( :has_many :belongs_to etc. ), and klass method to know the, em, class of the associated object(s).
Use reflect_on_all_associations class method on the User model. This will return an array of reflection objects. On those objects you can call macro method to find out the association type (:has_many, :belongs_to, etc.), and klass method to know the, em, class of the associated object(s).
Because they make common operations simpler and easier in your code. For example, consider a simple Rails application that includes a model for customers and a model for orders. Each customer can have many orders.
Now, suppose we wanted to add a new order for an existing customer. 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.
I would suggest doing it with SQL. You can do it several ways. Are you using MYSQL?
If you want to do with with 2 queries and then add the numbers together you can do something like this in SQL: select count(starrings. Id) from starrings where starrable_type='BlogPost' and starrable_id=#{blogpost. Id} and select count(starrings.Id) from starrings join replies on starrings.
Starrable_type='Reply' and starrable_id=replies. Id and replies. Blog_post_id=#{blogpost.
Id} Note: I didn't test this SQL and may have misspelled something or made a typo You can run SQL counts with 'Starring. Count_by_sql(your sql here)' then add the 2 numbers together. You can probably get this down to 1 SQL statement with a union, but I wouldn't bother.
I would suggest doing it with SQL. You can do it several ways. Are you using MYSQL?
If you want to do with with 2 queries and then add the numbers together you can do something like this in SQL: select count(starrings. Id) from starrings where starrable_type='BlogPost' and starrable_id=#{blogpost. Id} and select count(starrings.Id) from starrings join replies on starrings.
Starrable_type='Reply' and starrable_id=replies. Id and replies. Blog_post_id=#{blogpost.
Id} Note: I didn't test this SQL and may have misspelled something or made a typo. You can run SQL counts with 'Starring. Count_by_sql(your sql here)' then add the 2 numbers together.
You can probably get this down to 1 SQL statement with a union, but I wouldn't bother.
Assuming you have a polymorphic method stars on both the BlogPost and Reply that returns the Starring objects for each (you may not, but it makes sense to me that you should) then in blog_post. Rb you can do: def total_number_of_stars stars. Size + replies.
Inject(0) { |s,v| s.stars. Size += v.stars. Size } end Admittedly this will do quite a few queries behind the scenes, but that's kind of how Rails works.
If it does not perform well enough you can use find_by_sql with bespoke SQL.
I'm basically doing this now, but it's scaling terribly. Assuming that there's a way to do this in one or two queries, I'd prefer to let the database do the heavy lifting. Thanks though!
– O. Frabjous-Dey Feb 10 '09 at 23:37.
Does not work with :through associations. Does not work with :polymorphic associations. Associations are ignored.
Associations support the :dependent option. How the deletion is done. This option on the different specific association types.
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.