can I update activeRecord associations with Lifecycles?

14 views
Skip to first unread message

hobo_hippy

unread,
Oct 23, 2011, 8:29:13 AM10/23/11
to Hobo Users
I have three (really 4) classes working together: menus, items, orders
(and order_assists). Their associations are listed below. I have
lifecycles setup for items with two states, :in_order
and :not_in_order. I'm already showing items on the menu show page
(using auto actions for) and have edited the item cards such that the
transition button shows on the card. This is all great! However, I'm
shooting for having the transition associate the item to the current
user's order (I can just assume the current user has an order
already). I don't see any documentation in any tutorials or in the
manual on how to accomplish this.

Can it be done? Or do I want to do this another way? Create a custom
form somehow? Please advise!

menus:
has_many :items, :dependent => :destroy
has_many :orders

items:
belongs_to :menu
has_many :order_assist
has_many :orders, :through => :order_assist, :accessible => true

orders:
belongs_to :user, :creator => true
belongs_to :menu
has_many :order_assist
has_many :items, :through => :order_assist, :accessible => true

order_assists:
belongs_to :order
belongs_to :item
belongs_to :menu

-HoboHippy

hobo_hippy

unread,
Oct 23, 2011, 8:37:53 AM10/23/11
to Hobo Users
on further thought, should I define a function in the model and call
that function with the lifecycle? really, the problem I'm having is I
have no idea on how to "manually" associate any particular item with a
particular order. I hope someone has run into this sort of thing
before and has an example!!!

-hoboHippy

hobo_hippy

unread,
Oct 23, 2011, 9:23:18 AM10/23/11
to Hobo Users
...reading through the rails api...

My best guess (doing it the hard way) would be some fancy combination
of activerecord calls to:

1) create a new order_assist
2) set the order_id
3) set the item_id

is that all i'd have to do? What kind of syntax would I use inside a
lifecycle to get this to happen? would it be like what I suggest
below?

transition :add_to_order, {:not_in_order => :in_order},
:available_to => "User", do
<%= rails api calls %>
end

The more I read the more questions I have, so I'll stop here and see
what anyone might have to say!!

-hoboHippy

hobo_hippy

unread,
Oct 23, 2011, 10:42:26 AM10/23/11
to Hobo Users
OK, yet another thought (can't resist).

i ran rake routes and I found the create_order_assist route:

create_order_assist POST /order_assists(.:format)
{:action=>"create", :controller=>"order_assists"}

could I do something with this, perhaps create a button that uses a
customized controller action? I'm sure I would have to define a custom
action for create inside the order_assists_controller.


I'm full of ideas but don't really know how to accomplish any of these
things. If this last suggestion I've made is the way to go, how do I
go about creating a new order_assist? Assuming I know the order and
item id's i want to give the order_assist, how do I set them in the
new model instance as/after I create it?


-a very caffeinated hoboHippy

kevinpfromnm

unread,
Oct 23, 2011, 11:25:57 AM10/23/11
to hobo...@googlegroups.com
What you're probably looking for is the :params option for transitions though if the state is only whether it's in an order or not... you could do without a lifecycle or having the states being for only the parts after the item is added to an order.

hobo_hippy

unread,
Oct 24, 2011, 8:31:58 PM10/24/11
to Hobo Users
It's not as simple as getting an item to belong to an order. And item
has many orders through order assists, and through testing I've
figured out what I need to do is create an order_assist and set the
item_id and order_id. Can I do that with the :params? Or should I
create a form to just create it?

kevinpfromnm

unread,
Oct 24, 2011, 10:01:10 PM10/24/11
to hobo...@googlegroups.com
sounds more like a shopping cart where you're creating an order<->item link.  Unless you've got additional info on the link table, you possibly could get by with just a button (that submits a prefilled form behind the scenes).

hobo_hippy

unread,
Oct 25, 2011, 2:37:26 AM10/25/11
to Hobo Users
figured the easiest way to do this was with a create-button. Moving
ongoing conversation to a new post
Reply all
Reply to author
Forward
0 new messages