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!
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"?
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?
On Mar 5, 11:15 pm, "Luke Ivers" <technod...@gmail.com> wrote:
> 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?
Where did you get this knowledge? By reading the API?
On Mar 6, 1:52 am, "Luke Ivers" <technod...@gmail.com> wrote:
Thanks!