how to design database: has_and_belongs_to_many

I'm not sure how to design my database. This is the casus:

There are users who can make declarations. And there are users who are coupled to the declaration. They are the ones who have to pay for the declaration. So the first users did pay for something in a store. Enters it in the system and add other users to the declaration. The costs are splitted among the coupled users.

For example: user A bought a crate of Beer for €9. His roommates (B and C) are going to drink too and the all decide to share the costs. A enters the crate in the system and everyone has to pay €3.

This is an existing application but I'm going to remake it in Ruby. (A part of) the current structure is like this:

Table: declarations user_id amount description etc...

Table: declarations_users user_id declaration_id

Is it possible to have the user_id in the declarations table and in the many_to_many table? And does get this? Do I have to make an has_and_belongs_to_many declaration AND an belongs_to?Or just one?

Thanks in advance!

You can just… do what it is you’re trying to do.

class User has_and_belongs_to_many :declarations has_many :my_declarations, :class_name => ‘Declarations’ end

class Declaration has_and_belongs_to_many :users belongs_to :owning_user, :class_name => ‘User’ end

Now you refer to declaration.users to find all the users that belong to it You refer to declaration.owning_user to find out the original owner

You refer to user.declarations to find all the declarations that belong to a user You refer to user.my_declarations to find all the declarations that are owned by that user

This should work given the table descriptions in your email.

Ah thanks! I think this is exactly what I want.

I'm starting with Ruby on Rails, so maybe a dumb question: my_declarations and owning_user is an alias? So the db table field (for example) is still called "user_id"?

Right… basically, the first part of declaring an association is telling it what name to give it in the class… If they didn’t set up default values for you, then EVERY declaration would look like has_many :users, :class_name => ‘User’, foreign_key => ‘user_id’

But that’s the whole “convention over configuration” thing they talk about in the Rails community… since the chances are that 99% of the time, those are going to be right, it just has them set as defaults.

But, you can ALWAYS specify them, if you feel the need to, and especially if you’re needing to associations to the same object… you just use different first arguments, and then specify class_name and foreign_key if you have to.

You’re welcome… and there are no dumb questions: just relative levels of knoweldge.

Thanks again for the nice reply. Is everyone that nice at the email lists?

With this line: has_many :users, :class_name => 'User', foreign_key => 'user_id' you can access the users with the instance variable @users in the controller?

I have created this table (for deposits from one user to another to pay for depts).

class CreateDeposits < ActiveRecord::Migration   def self.up     transaction do       create_table :deposits do |table|         table.column :from_user_id, :integer         table.column :to_user_id, :integer         table.column :amount, :integer         table.column :confirmed, :boolean         table.column :created_at, :datetime         table.column :updated_at, :datetime       end     end

  def self.down     drop_table :deposits   end end

with this line the logic would be complete if I understand it all corectly? belongs_to :payer, :class_name => 'User', foreign_key => 'from_user_id' belongs_to :casher, :class_name => 'User', foreign_key => 'from_user_id'

Where did you get this knowledge? By reading the API?

Thanks again for the nice reply. Is everyone that nice at the email lists?

The Rails mailing list is surprisingly polite and helpful.

With this line: has_many :users, :class_name => ‘User’, foreign_key => ‘user_id’ you can access the users with the instance variable @users in the controller?

I have created this table (for deposits from one user to another to

pay for depts).

class CreateDeposits < ActiveRecord::Migration def self.up transaction do create_table :deposits do |table| table.column :from_user_id, :integer table.column :to_user_id, :integer table.column :amount, :integer table.column :confirmed, :boolean table.column :created_at, :datetime table.column :updated_at, :datetime end end

def self.down drop_table :deposits end end

with this line the logic would be complete if I understand it all corectly? belongs_to :payer, :class_name => ‘User’, foreign_key =>

‘from_user_id’ belongs_to :casher, :class_name => ‘User’, foreign_key => ‘from_user_id’

I believe you intended one of those to be ‘to_user_id’ as opposed to bother ‘from_user_id’, but other than that, yes it’s correct.

Where did you get this knowledge? By reading the API?

I got it just like this: trying to build something, not understanding, and either asking questions, or looking up the answer to specific questions in the API… just generally trying to read the API or really any other source of information without a specific example that you’re working towards will usually only get you 1/4 of the way to understanding what you’re doing… actually using the information is what makes you really understand. :slight_smile:

Ah, thanks. I'm gonna try to make this work tomorrow. With a bit of luck I've got a working application some weeks from now.

It just don't get it. I thought the extra relationships would auto- generate some extra variables. So let's say I do "declarations/show/1" and I would like to see all the coupled users (from declarations_users). How do I access them in the view? Do I have to edit some additional controller code?

Thanks!

Ahhhhh.... never mind. I got it! I read this sourcecode and found out that: @declaration.owner @declaration.users would work. And it dit!