Associations ownership

3 views
Skip to first unread message

Gabriele Tassoni

unread,
May 21, 2009, 10:07:52 AM5/21/09
to Hobo Users
Hi,
I'm experimenting with the ownership in hobo, the app has an invoice
module and a bank module, both with the ownership in place, thus:

class Bank < ActiveRecord::Base

hobo_model # Don't put anything above this

# SOME CODE

# --- Relations --- #
belongs_to :owner, :class_name => "User", :creator => true
has_many :invoices, :dependent => :destroy

# SOME CODE
end

and for the invoice:

class Invoice < ActiveRecord::Base

hobo_model # Don't put anything above this

# SOME CODE

belongs_to :bank
belongs_to :owner, :class_name => "User", :creator => true
# SOME CODE
end

the users.rb has:

has_many :banks, :class_name => "Bank", :foreign_key =>
"owner_id", :dependent => :destroy
has_many :invoices, :class_name => "Invoice", :foreign_key =>
"owner_id", :dependent => :destroy

what I would expect is that the index of both invoices and banks just
shows the current_user's invoices and banks, this is easy, before
reading the Agility tutorial, I redifined the index method like this
(i.e. for Invoice):

def index
hobo_index Invoice.owned_by(current_user), :per_page => 10, :order
=> "issued_on DESC, number DESC"
end

As you can see I had to define a owned_by named_scope for all the
models, not really dry, but it worked.
after reading the tutorial I understood that the correct way would be
to redifine part of the dryml of the index pages, making the
collection look like this:

<collection:invoices with="&current_user"/>

I have to test it, but it looks okay, now comes the problem, I can't
understand the right way to achieve the same behaviour for select
boxes that represent and association, I mean, if I create a new
Invoice, I have a select that lists the banks one could choose, but I
can see all the banks, even the ones owned by a different user than
me... which is the right hobo/DRY way to achieve this? I don't think I
have to redefine the select tag... or do I have? °_°
And what wuold happen in the case of a has_many :through? I think I'm
missing something important, but can't see what it is!

Thank you
G.

Bryan Larsen

unread,
May 21, 2009, 12:02:11 PM5/21/09
to hobo...@googlegroups.com
> <collection:invoices with="&current_user"/>

I think that you would be better served by using "owner-actions"

If you add "auto_actions_for :owner, [:index]" to your
invoicecontroller, you can get the list of user 1's invoice with
"/users/1-admin/invoices". See the end of "Granting Read Access to
Others" for an example.

For your actual question you can use something like:

<extend tag="form" for="Invoice">
<old-form merge>
<field-list:>
<bank-view:>
<select-one options="&Bank.owner_is(this_parent.owner)"/>
</bank-view:>
</field-list:>
</old-form>
</extend>


Bryan
Reply all
Reply to author
Forward
0 new messages