Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generating model with ActiveRecord associations

Folks,

I am working on creating an application where I have two entities in my problem space. One entity is "biologist" and the other entity is "experiment" now one biologist can have many experiments and each experiment can have many biologist.

I know how to create models and routes using the command line generator:

 rails generate scaffold Biologist name:string expertise:string last_pub:text

What is the right way to add an association? Is it to go and update the model code after generation? What is unclear to me is if I add a "belongs_to" association post generation how does that reflect in the DB schema without running a migration or something? In the above example if "experiment" belongs to "biologist" then there will be a foreign key in the "biologist" table, how will that be created if I add the associations in the model class post generation. I am fairly new to rails, so I apologize if this is a naive question.

like image 433
user1781472 Avatar asked Feb 19 '13 13:02

user1781472


People also ask

What are ActiveRecord methods?

Active Record allows you to validate the state of a model before it gets written into the database. There are several methods that you can use to check your models and validate that an attribute value is not empty, is unique and not already in the database, follows a specific format, and many more.

What is the difference between Has_one and Belongs_to?

They essentially do the same thing, the only difference is what side of the relationship you are on. If a User has a Profile , then in the User class you'd have has_one :profile and in the Profile class you'd have belongs_to :user . To determine who "has" the other object, look at where the foreign key is.

How do you create a many-to-many relationship in Rails?

In Rails, there are two main ways of creating many-to-many relationships between models: using a has_and_belongs_to_many association. using has_many :through association.

How to make efficient use of active record associations in rails?

Here are a few things you should know to make efficient use of Active Record associations in your Rails applications: 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. For example:

What are transitive many-to-many relationships in ActiveRecord?

Such relationships can be called transitive many-to-many as we rely on the presence of other entities to fully capture the semantics of the relationship. Luckily, ActiveRecord allows us to model such relationships with ease. Let’s start by looking at the simplest ActiveRecord many-to-many associations and work our way up.

How to get the model name from the Association name?

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. For example, if a part has many assemblies, but the actual name of the model containing assemblies is Gadget, you'd set things up this way:

How do I create an association in the supplier model?

Each instance of the Supplier model will have these methods: When initializing a new has_one or belongs_to association you must use the build_ prefix to build the association, rather than the association.build method that would be used for has_many or has_and_belongs_to_many associations. To create one, use the create_ prefix.


1 Answers

I think you are looking for something like the following:

rails g scaffold Biologist experiment:references

Read this article by Jose Valim where he shows you how to do the above, and also add db indexes from the command line:

like image 172
stephenmurdoch Avatar answered Oct 11 '22 11:10

stephenmurdoch