0

I'm relatively new to rails and very new to database manipulation.

I'm trying to create a class within the database which contains a number of custom objects within it. These custom objects are also to be stored in the database in a separate table. I've managed to set this up as follows

class MyClass < ActiveRecord::Base
  has_many :other_objects, :dependent => destroy
end

class OtherObject < ActiveRecord::Base
  belongs_to :my_class
  attr_accessible :some_stuff...
end

I've created the appropriate database tables and managed to get it working.

Now what I want to do is have (four) particular instances of "OtherObject"s in my class, which can be accessed by some straightforward identifier, something like

test = MyClass.new
...
test.instance_of_other_object.some_attribute = "blahblah"

Such that this updates the database entry for the associated object. What is the best way to go about this?

3 Answers 3

1

That has_many association sets up MyClass#other_objects (and a bunch of other methods) to allow you to easily work with associated records.

You probably want:

my_class.other_objects.each do |other_object|
  other_object.update_attributes(:foo => 'bar')
end

If you want a direct SQL update, you can use update_all:

my_class.other_objects.update_all(:foo => 'bar')

Update:

If that's the sort of association you need, you may define a belongs_to association:

class MyClass < ActiveRecord::Base
  has_many :other_objects, :dependent => :destroy

  # uses :selected_other_object_id
  belongs_to :selected_other_object, :class_name => "OtherObject"
end

my_class = MyClass.first
my_class.selected_other_object = other_object  # Set the object.
# => #<OtherClass:...>
my_class.selected_other_object_id     # ID has been set.
# => 10
my_class.selected_other_object        # Retrieve the object.
# => #<OtherClass:...>
my_class.selected_other_object.save   # Persist ID and other fields in the DB.

my_class = MyClass.find(my_class.id)  # If you fetch the object again...
# => #<MyClass:...>
my_class.selected_other_object_id     # The ID is still there.
# => 10
my_class.selected_other_object        # You have the ID, you get the object.
# => #<OtherClass:...>

my_class.selected_other_object.foo = "bar"  # Access associated object this way.
another_variable = my_class.selected_other_object  # Or this way.

Remember however that this does not assume that :selected_other_object is a subset of :other_objects.

Also note that the selected_other_object and selected_other_object= methods are already set up when you set up the association, so you don't have to define these yourself.

Sign up to request clarification or add additional context in comments.

6 Comments

That's not quite what I'm looking for. I want to get and set a single particular instance of the other_object. So my_class.this_special_object_that_i_can_refer_to_from_this_special_identifier.some_attribute = "whatever"
I updated the answer. It might be closer to what you need now.
Yes that works in terms of updating the OtherObject in the database. Thank you. However, :selected_other_object itself is not stored in the db so it needs to be pointed towards an entry in the database whenever it is first used in the application. I haven't yet come up with a neat way of doing this
Please see the update sample code in the answer. The selected_other_object= method that your belongs_to application set up already sets the selected_other_object_id in your MyClass object, and would persist this value if you save your MyClass object.
This makes no sense to me, even if it works. If you look at the way I have set up the db tables and at the class definitions, surely an 'OtherObject' belongs to a 'MyClass' object. It is clearly shown here (guides.rubyonrails.org/association_basics.html) that for the belongs-to association, the object that belongs to another should have the id of the object it belongs to, which, in this case, is the other way around. On a separate issue, it also would surely not work if you were to add multiple belongs_to :selected_other_object of the same class type, as I am trying to do.
|
0

This is not a complete answer but I have come up with something that works for getting the object, but not for setting it.

class MyClass < ActiveRecord::Base
  has_many :other_objects, :dependent => destroy

  def particular_instance
    return OtherObject.find_by_id(self.particular_instance_id)
  end
end

Where my db schema looks like this

create_table "my_classs", :force => true do |t|
  t.integer "particular_instance_id"
end

create_table "other_objects", :force => true do |t|
  t.integer "my_class_id"
  t.string "some_attribute"
end

Update In order to set an attribute of the other_object class the update_attributes method can be used

my_class.particular_instance.update_attributes(:some_attribute => "blah")

Comments

0

Use object.dup now for rails 3.1 and on.

a = MyClass.first # finds the first instance of the model "MyClass"  

b = a.dup # duplicates the record.

c = a.dup # duplicates the record again.  

b.field_name = "blahblah"

c.fielf_name = "blahblah"

b.save # saves the record into the database for object b.

c.save # saves the record into the database for object c.

If you look into your database, you can see that a new record has been created. It is identical to the first record excepted that it has a new id.

Also check Duplicate a model for more information about it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.