Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating or updating a has_one ActiveRecord association

I've been trying to get my head around ActiveRecord associations but I have hit a bit of a brick wall, and no matter how much I review the ActiveRecord documentation, I can't work out how to solve my problem.

I have two classes:

Property -> has_one :contract   Contract -> belongs_to :property 

In my contract class, I have a method to create_or_update_from_xml

First I check to make sure the property in question exists.

property_unique_id = xml_node.css('property_id').text       property = Property.find_by_unique_id(property_unique_id)       next unless property 

And this is where I get stuck, I have a hash of attributes for the contract, and what I want to do is something like:

if property.contract.nil?   # create a new one and populate it with attributes else   # use the existing one and update it with attributes 

I know how I would go about it if it was raw SQL, but I can't get my head around hte ActiveRecord approach.

Any hints past this road block would be hugely appreciated.

Thanks in advance.

like image 672
Still Real Avatar asked Jan 13 '12 00:01

Still Real


People also ask

How would you choose between Belongs_to and Has_one?

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.

What is a polymorphic association in Rails?

In Ruby on Rails, a polymorphic association is an Active Record association that can connect a model to multiple other models. For example, we can use a single association to connect the Review model with the Event and Restaurant models, allowing us to connect a review with either an event or a restaurant.

What is Association in Ruby on Rails?

Association in Rails defines the relationship between models. It is also the connection between two Active Record models. To figure out the relationship between models, we have to determine the types of relationship. Whether it; belongs_to, has_many, has_one, has_one:through, has_and_belongs_to_many.

What is dependent destroy in Rails?

Dependent is an option of Rails collection association declaration to cascade the delete action. The :destroy is to cause the associated object to also be destroyed when its owner is destroyed.


2 Answers

if property.contract.nil?   property.create_contract(some_attributes) else   property.contract.update_attributes(some_attributes) end 

Should do the trick. When you have a has_one or belongs_to association then you get build_foo and create_foo methods (which are like Foo.new and Foo.create). If the association already exists then property.contract is basically just a normal active record object.

like image 178
Frederick Cheung Avatar answered Sep 28 '22 03:09

Frederick Cheung


Just another way of doing it using ruby OR-Equal trick

property.contract ||= property.build_contract property.contract.update_attributes(some_attributes) 

Update:

@KayWu is right, the above ||= trick will create the contract object in the first line, rather than just building it. An alternative would be

property.build_contract unless property.contract property.contract.update_attributes(some_attributes) 
like image 30
Kinaan Khan Sherwani Avatar answered Sep 28 '22 02:09

Kinaan Khan Sherwani