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.
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.
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.
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With