dkubb changed the topic of #datamapper to: Datamapper v1.2.0 | Mailing List: http://is.gd/aa9D | Logs: http://is.gd/qWAL7V | DataMapper 2 Renamed to ROM, see #rom-rb for development
cored has quit [Ping timeout: 246 seconds]
snusnu has quit [Quit: Leaving.]
knowtheory has quit [Quit: Computer has gone to sleep]
zombor has quit [Remote host closed the connection]
bobocopy has joined #datamapper
zombor has joined #datamapper
bobocopy has quit [Quit: Leaving.]
dkubb has joined #datamapper
zombor has quit [Remote host closed the connection]
ckrailo has quit [Quit: Computer has gone to sleep.]
postmodern has quit [Quit: Leaving]
ckrailo has joined #datamapper
Ortuna has quit [Quit: Computer has gone to sleep.]
ckrailo has quit [Quit: Computer has gone to sleep.]
rsim has joined #datamapper
rsim has quit [Ping timeout: 240 seconds]
swarmhost has joined #datamapper
<swarmhost>
Can someome help me out building a basic set of models with a few relationships? I'm having trouble wrapping my head around it - first ORM I have ever used
<dkubb>
this is kind of like private chat, and I tend not to want to provide help via private chat unless the information is sensitive. public discussion is usually better because this channel is logged and it can sometimes help others via google
<dkubb>
ok one sec
<swarmhost>
yeah, I don't use the chat part of collabedit - just sometimes it's easier for helpers to be able to move around in the code and edit stuff on the fly. :) everyone has their own preference :) I appreciate the help
<dkubb>
swarmhost: ok, on the Event side you'd probably get two foreign keys like createdby_id and createdfor_id
<dkubb>
depending on how you're loading the models, you may want to use a Symbol for the model name
<swarmhost>
a symbol?
<dkubb>
also, probably not an issue, but the second argument to belongs_to is the model name, eg: belongs_to :created_by, :Person
<swarmhost>
Dir[File.dirname(__FILE__) + '/models/[A-Z]*.rb'].each {|f| require f }
<dkubb>
actually, scratch that, use 'Person'
<swarmhost>
that's how I am loading models
<dkubb>
sorry, I was thinking about something else.. another orm system I'm working on
<swarmhost>
no worries
<dkubb>
ahh I see, so you're basically requiring them all at once up-front
<swarmhost>
yes
<dkubb>
you may have issues using the actual constant before it's been loaded. like say Event is required before Person, the Person constant won't exist yet
<dkubb>
that's why I said use a String. DM will figure it out when the associations are finalized
<dkubb>
so aside from using Strings with the associations, I assume you probably don't know how to associate Person -> Event
<swarmhost>
The "use a string".. did I miss something above? I think your only mention is mentioning you mentioned it..
<dkubb>
declare those belongs_to relationships like this:
<dkubb>
belongs_to :createdby, 'Person'
<dkubb>
belongs_to :createdfor, 'Person'
<dkubb>
I meant when you pass in the model to belongs_to use a string for the name of the model, not a real constant
<swarmhost>
ah, gotcha
<swarmhost>
ok, I added that to Events.rb
<dkubb>
the constant may or may not be defined at the point in time the belongs_to line is evaluated
<dkubb>
if it's not defined it'll blow up on you
<swarmhost>
var/lib/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/property.rb:366:in `<': comparison of String with Class failed (ArgumentError)
<swarmhost>
thats what my test shows
<dkubb>
are you able to update that pastie with what you have or paste a new link?
<swarmhost>
sure, I'll do that
<swarmhost>
all I added was property :createdby, 'Person' and property :createdfor, 'Person' in the Event.rb
<dkubb>
note I said belongs_to above ;)
<swarmhost>
do'h!
<swarmhost>
ok, the model successfully loads now
<dkubb>
sweet
<dkubb>
so that's the first part, linking back from the Person to the Event is a bit weird
<swarmhost>
thats for the production, not the test suite
<dkubb>
what I would suggest is for tests use automigrate
<swarmhost>
dbsetup('sqlite::memory:', 'replace')
<dkubb>
it will tear everything down and rebuild it all
<swarmhost>
that's for my test ^
<dkubb>
cool
<dkubb>
what kind of failure are you seeing?
<swarmhost>
so, it is migrating - thats how I understood it to work so thats why I wrote the dbsetup() stuff
<swarmhost>
Event not saved
<swarmhost>
when adding test event
<swarmhost>
but it doesn't know what person to add it to, as I don't have people
<swarmhost>
i s'pose
<dkubb>
yeah, by default the foreign key will be NOT NULL
<dkubb>
so those two FK's in Event will be NOT NULL
<dkubb>
you can override that by specifying :required => false in the belongs_to statement
<dkubb>
although in general I would recommend against removing constraints unless it really is a valid state for an event to not be created by or for someone
<swarmhost>
yeah the constraint need to be there. to confirm though, it's a good troubleshooting step. I did required=> false, and the tests pass, so the ability is there. now I just need to write my tests better. :)
<dkubb>
you can also create an event with a person doing something like: Event.create(:person => { ... required fields for the person ... }, ...)
<dkubb>
you could have a hash of valid person attributes somewhere that you reuse
<dkubb>
or you can have a person created
<swarmhost>
yeah, I'm creating a John Doe at the start of the tests
<dkubb>
whoops, sorry, I forgot there's no :person relationship in your Event model. I guess your relationships are :createdby and :createdfor
<dkubb>
and yeah, since there's two relationships, creating a John Doe up-front and using it for both is probably better
<dkubb>
less work in the db == faster integration tests