Rails: How to use a belongs_to association for referencing in addition to a has_many association?

Although I agree with lukewendling, the alternative is to establish an association in Address (to User ) and then use a before_save to update the user_id if needed. Something like this: has_one :main_user, :class_name => 'User', :foreign_key => 'main_address_id' before_save :update_user_id def update_user_id if main_user. Present?Self.

User_id = main_user. Id end end.

Although I agree with lukewendling, the alternative is to establish an association in Address (to User) and then use a before_save to update the user_id if needed. Something like this: has_one :main_user, :class_name => 'User', :foreign_key => 'main_address_id' before_save :update_user_id def update_user_id if main_user. Present?Self.

User_id = main_user. Id end end.

Your modeling of the 'real' world seems a bit odd, and similarly, the association you're trying to make is complicated. A User doesn't seem to belong to an MainAddress any more than a User belongs to her PrimaryVehicle. Why not have a boolean on the Address model such as Address#primary?

Sure, this would be a solution, but I saw this approach in another software project and think it is smarter than just use a boolean attribute. So I hope anybody else has an idea... – MMore Jul 14 at 17:52.

All of the association methods are built around caching, which keeps the result of the most recent query available for further operations. The cache is even shared across methods. But what if you want to reload the cache, because data might have been changed by some other part of the application?

You are not free to use just any name for your associations. Because creating an association adds a method with that name to the model, it is a bad idea to give an association a name that is already used for an instance method of ActiveRecord::Base. The association method would override the base method and break things.

For instance, attributes or connection are bad names for associations. Associations are extremely useful, but they are not magic. You are responsible for maintaining your database schema to match your associations.

In practice, this means two things, depending on what sort of associations you are creating. For belongs_to associations you need to create foreign keys, and for has_and_belongs_to_many associations you need to create the appropriate join table. When you declare a belongs_to association, you need to create foreign keys as appropriate.

If you create an association some time after you build the underlying model, you need to remember to create an add_column migration to provide the necessary foreign key. If you create a has_and_belongs_to_many association, you need to explicitly create the joining table. Unless the name of the join table is explicitly specified by using the :join_table option, Active Record creates the name by using the lexical order of the class names.

So a join between customer and order models will give the default join table name of "customers_orders" because "c" outranks "o" in lexical ordering. Whatever the name, you must manually generate the join table with an appropriate migration. These need to be backed up by a migration to create the assemblies_parts table.

We pass id: false to create_table because that table does not represent a model. That's required for the association to work properly. If you observe any strange behavior in a has_and_belongs_to_many association like mangled models IDs, or exceptions about conflicting IDs, chances are you forgot that bit.

By default, associations look for objects only within the current module's scope. This can be important when you declare Active Record models within a module. This will work fine, because both the Supplier and the Account class are defined within the same scope.

By default, Active Record doesn't know about the connection between these associations. This happens because c and o. Customer are two different in-memory representations of the same data, and neither one is automatically refreshed from changes to the other.

They do not work with :through associations. They do not work with :polymorphic associations. They do not work with :as associations.

For belongs_to associations, has_many inverse associations are ignored. The following sections give the details of each type of association, including the methods that they add and the options that you can use when declaring an association. The belongs_to association creates a one-to-one match with another model.

In database terms, this association says that this class contains the foreign key. If the other class contains the foreign key, then you should use has_one instead. In all of these methods, association is replaced with the symbol passed as the first argument to belongs_to.

The association method returns the associated object, if any. If no associated object is found, it returns nil. If the associated object has already been retrieved from the database for this object, the cached version will be returned.

To override this behavior (and force a database read), pass true as the force_reload argument. The association= method assigns an associated object to this object. Behind the scenes, this means extracting the primary key from the associate object and setting this object's foreign key to the same value.

The build_association method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through this object's foreign key will be set, but the associated object will not yet be saved. The create_association method returns a new object of the associated type.

This object will be instantiated from the passed attributes, the link through this object's foreign key will be set, and, once it passes all of the validations specified on the associated model, the associated object will be saved. While Rails uses intelligent defaults that will work well in most situations, there may be times when you want to customize the behavior of the belongs_to association reference. Such customizations can easily be accomplished by passing options and scope blocks when you create the association.

If you set the :autosave option to true, Rails will save any loaded members and destroy members that are marked for destruction whenever you save the parent object. If the name of the other model cannot be derived from the association name, you can use the :class_name option to supply the model name. The :counter_cache option can be used to make finding the number of belonging objects more efficient.

With these declarations, asking for the value of @customer.orders. Size requires making a call to the database to perform a COUNT(*) query. With this declaration, Rails will keep the cache value up to date, and then return that value in response to the size method.

Although the :counter_cache option is specified on the model that includes the belongs_to declaration, the actual column must be added to the associated model. In the case above, you would need to add a column named orders_count to the Customer model. Counter cache columns are added to the containing model's list of read-only attributes through attr_readonly.

If you set the :dependent option to :destroy, then deleting this object will call the destroy method on the associated object to delete that object. If you set the :dependent option to :delete, then deleting this object will delete the associated object without calling its destroy method. By convention, Rails assumes that the column used to hold the foreign key on this model is the name of the association with the suffix _id added.

The :inverse_of option specifies the name of the has_many or has_one association that is the inverse of this association. Does not work in combination with the :polymorphic options. Passing true to the :polymorphic option indicates that this is a polymorphic association.

Polymorphic associations were discussed in detail earlier in this guide. In this case, saving or destroying an order will update the timestamp on the associated customer. If you set the :validate option to true, then associated objects will be validated whenever you save this object.

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