DataMapper- can I avoid intermediate tables? -
i total beginner @ datamapper, , have 2 models:
class thirdpartyaccount include datamapper::resource property :access_token, string, :length => 500 belongs_to :user end class user include datamapper::resource property :id, serial property :first_name, string has n, :third_party_accounts, :through => resource end
looking @ sql logs, appears create 2 tables- users
, third_party_accounts
, third_party_account_users
join two. doesn't appear last table needed- surely third_party_account
table needs use it's user_id
field map directly user
table? have accidentally created many-to-many relationship here?
it's due line:
has n, :third_party_accounts, :through => resource
:through => resource
tells datamapper it's "has-and-belongs-to-many" relation (each 3rd party account belongs multiple users , each user has multiple 3rd party accounts), requires intermediate table. if has-many relation (each user has many 3rd party accounts, each account belongs 1 user), should use:
class user ... has n, :third_party_accounts end
see http://datamapper.org/docs/associations.html more info.
Comments
Post a Comment