Noob troubles with has_many

10 views
Skip to first unread message

Ben

unread,
Oct 11, 2010, 12:55:38 PM10/11/10
to SimpleRecord
Hi

I'm having some trouble with the basics - specifically with creating a
has_many relationship. Help on understanding why the below does not
work, much appreciated.

cheers,
--Ben

class Able < SimpleRecord::Base
has_attributes :foo
has_many :bravos
end

class Bravo < SimpleRecord::Base
has_attributes :dings
end

a = Able.create(:foo => "bar", :bravos => [ ])
a.bravos << Bravo.create(:dings => "bums")
a.save
a.reload
y a

--- !ruby/object:Able
attributes:
id: 25b41c8c-d558-11df-8f61-00254bfffec5
created:
- 2010-10-11T16:54:01
updated:
- 2010-10-11T16:54:02
foo:
- bar
attributes_rb:
foo: bar
created: 2010-10-11T16:54:01+00:00
updated: 2010-10-11T16:54:02+00:00
dirty: {}

errors: !ruby/object:SimpleRecord::SimpleRecord_errors
errors: {}

lobs: {}

new_record: false

Travis Reeder

unread,
Oct 11, 2010, 4:30:49 PM10/11/10
to simple...@googlegroups.com
Do you mean by "does not work" that the Bravo that you create is not being attached to the Able?


--
You received this message because you are subscribed to the Google Groups "SimpleRecord" group.
To post to this group, send email to simple...@googlegroups.com.
To unsubscribe from this group, send email to simple-recor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/simple-record?hl=en.


Ben Skelton

unread,
Oct 11, 2010, 10:23:58 PM10/11/10
to simple...@googlegroups.com
Hi Travis,

> Do you mean by "does not work" that the Bravo that you create is not being attached to the Able?

exactly. If I've done everything right up until this point, how do I access the Bravo object from Able?

cheers,
--Ben

Travis Reeder

unread,
Oct 16, 2010, 7:39:07 PM10/16/10
to simple...@googlegroups.com
It's possible that it's not fully supported or it's a bug. Any chance you could debug it and see where it's failing?  My guess is it's this line:

a.bravos << Bravo.create(:dings => "bums")

Remember, SimpleDB is slow and you should avoid database hits as much as possible. With your code, you create the Able object (one db hit), then you create the Bravo object (two db hits), then add it to the bravos array, then save a (third db hit) which in turn should go and save the changes to the bravos array (four db hits). There's extra db hits in there which is bad.

You may just want to try changing your code to: 

a = Able.create(:foo => "bar")
b = Bravo.create(:dings => "bums", :able=>a)

Then that's two db hits total to accomplish the same thing. 
Reply all
Reply to author
Forward
0 new messages