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
rsim has joined #datamapper
rsim has quit [Ping timeout: 245 seconds]
lgierth has quit [Ping timeout: 248 seconds]
rsim has joined #datamapper
rsim has quit [Ping timeout: 245 seconds]
lfox has quit [Quit: ZZZzzz…]
rsim has joined #datamapper
rsim has quit [Ping timeout: 245 seconds]
akasper has joined #datamapper
lfox has joined #datamapper
zombor has joined #datamapper
akasper has quit [Quit: Leaving.]
zombor has quit [Remote host closed the connection]
lfox has quit [Quit: ZZZzzz…]
rsim has joined #datamapper
rsim has quit [Quit: Leaving.]
mikecmpbll has joined #datamapper
postmodern has quit [Quit: Leaving]
skade has joined #datamapper
skade has quit [Quit: Computer has gone to sleep.]
<jordanyee>
If I have a model for Location (address, city, etc) and have two different models like Business and Customer, how should the relationship be set up?
<onewheelskyward>
That depends.
<onewheelskyward>
Does the business have a location, or the customer? Or both?
<jordanyee>
Both business and customer can have 1 (has 1) location
<onewheelskyward>
Ah then I'd have two belongs_to statements. With, I think, optional flags.
<onewheelskyward>
That might work if you want to discriminate between business and customer locations.
<jordanyee>
Ah yes
<jordanyee>
Hmm that gives me something to think about
<onewheelskyward>
I've done that before, it works well.
<jordanyee>
What if I don't ever intend to access locations by themselves, only through a Business or Customer?
<jordanyee>
Could I get away with only having Business and Customer having a has 1 relationship?
<onewheelskyward>
Oh for sure
<onewheelskyward>
Keep it simple.
<jordanyee>
Oh good, simple's what I'm aiming for
romanin has quit [Ping timeout: 260 seconds]
<jordanyee>
Assuming Business has a has 1 relationship to Location, what's the proper order for creation? Business then Location or Location then Business?
<jordanyee>
I tried to create a Location but got the error that "Business must not be blank"
<onewheelskyward>
Go up the belongs_to chain.
<onewheelskyward>
If location belongs_to business, business comes first.
<jordanyee>
My thinking is going down the has 1 chain, which leads me to think a Location has to exist to give it to a Business
<jordanyee>
Is that wrong?
<jordanyee>
(Thanks for the help by the way)
<onewheelskyward>
That is wrong, in fact.
<onewheelskyward>
The belongs_to is essentially a foreign key constraint.
<onewheelskyward>
So the has_1 object must exist before it can have 1. Make sense?
<onewheelskyward>
Create the business, then you can Location.new(business: my_business)
<jordanyee>
That makes sense given I'm using belongs_to, but if I'm not putting belongs_to on Location (to keep it simple)?
<onewheelskyward>
That's an interesting thought.
<jordanyee>
It seems like when Business has 1 Location, it's adding the foreign key to Location instead of Business
<onewheelskyward>
I guess you could reverse the relationship.
<onewheelskyward>
If business belonged to a location, the key would be on the business.
<onewheelskyward>
And customer, similarly
<jordanyee>
I just tried that actually, and that's what it did
<jordanyee>
It reads so wrong to me, Business belonging to Location
<onewheelskyward>
Well, it is. :)
<onewheelskyward>
From an architectural point of view.
<onewheelskyward>
But if it does what you need it to do...
<onewheelskyward>
Also, if you're not utilizing locations independently, why aren't they just fields on the business/customer?
<jordanyee>
Headaches like these aside, it seemed like a DRYer solution
<onewheelskyward>
I hear you on that, but unless you're making the data common it might make more sense to combine them.
<onewheelskyward>
It all depends on your use case.
<onewheelskyward>
And the amount of data you're dealing with.
<jordanyee>
Just so I'm sure, what do you mean by "making the data common"?
<onewheelskyward>
For instance, if a Business has location 123 main street and a Customer has location 123 main street, they point to the same location object.
<onewheelskyward>
Actually, if you skip the belongs_to in Location and use first_or_create for Location, you might be ok.
<jordanyee>
Oh oh, okay yeah
<onewheelskyward>
wait...that might not be ok, actually
<onewheelskyward>
Because if Business 1 and Customer 2 have the same location object, and Customer 2 changes it- Business 1 would also be changed.
<onewheelskyward>
So, don't change anything, only create new. :)
<jordanyee>
Oh right! Yeah!
<jordanyee>
Thanks so much onewheelskyward
<onewheelskyward>
You bet!
mbj has joined #datamapper
rsim has quit [Quit: Leaving.]
Eiam has quit [Ping timeout: 260 seconds]
akasper1 has quit [Ping timeout: 246 seconds]
lnormous has joined #datamapper
Eiam has joined #datamapper
<jordanyee>
onewheelskyward: Still around?
<onewheelskyward>
Yeah
<jordanyee>
Given the above... Is there a simple way to call .to_json on a Business and have it include the location?
<onewheelskyward>
When you pull the Business and inspect it, does it have a location node?
<jordanyee>
One second, let me check
<jordanyee>
Give me a few minutes... something broke
<onewheelskyward>
oops. :)
<jordanyee>
Okay! Sorry yes, it does. It has "@location_id=2"