1

I have a User model that has billing_id. I have Order model that runs transactions with a payment gateway which returns a billing id I'd like to save into the billing_id column on the User model. I think I am getting the basics of MVC architecture mixed up.

I am under the impression that UsersController and OrdersController update data of their respective tables. Does that mean that if I have something returned from OrdersController such as a billing id, there is no other way of saving that billing id into a billing_id column in User model? Thanks and sorry if this is extremely rudimentary. :)

Also, I thought a possible solution might be to somehow pass in the return value via ApplicationsController into UsersController to save into User table. Is this possible?

railslearner
  • 1,761
  • 2
  • 14
  • 18

1 Answers1

1

Your user orders table should have an instance of user_id, as a user can have multiple orders.

You can do this by creating a migration:

rails g migration add_user_id_to_orders order_id:integer
rake db:migrate

Your models will then look like:

class User < ActiveRecord::Base
  has_many :orders
end

class Order < ActiveRecord::Base
  belongs_to :user
end

You need a link between the two (order_id) otherwise they would have no knowledge of each other. This is known as a foreign key.

When things are set up this way, you can get the users by doing:

User.find(1).orders

And you can find the user information from an order by doing:

Orders.find(1).user

I hope this helps.

EDIT:

Orders.find(ORDER_ID).user.update_attributes(:billing_id => BILLING_ID)
Gazler
  • 83,029
  • 18
  • 279
  • 245
  • Thanks Gazler. I already set it up this way. Currently, I am concerned with how I might be able take a return value of a new order creation and save into the User table from OrdersController. Thanks for the input – railslearner Apr 07 '11 at 20:54
  • Why would you need to save this value to the user? Is it not associated to the Order? Could you please clarify? – Gazler Apr 07 '11 at 20:55
  • Yes. billingID is returned when creditcard number is saved in payment gateways database. I wanted to have billingID column in User table and save it there instead of in a Order table because billingID is returned only when it's a new credit card being saved. – railslearner Apr 07 '11 at 21:00
  • Thank you very much. So is it possible to update an attribute of User table from OrdersController? I think it's a fundamental MVC issue that I haven't been able to grasp well. – railslearner Apr 07 '11 at 21:05
  • I assume in your OrdersController you have an order variable. So replace Orders.find(ORDER_ID) with @order. – Gazler Apr 07 '11 at 21:09
  • I am sorry, but if I do update_attributes from OrdersController, doesn't it only relate to attributes of the Order model? As in if I ran Orders.find(ORDER_ID).user.update_attributes(:billing_id => BILLING_ID) it'd be looking for :billing_id in Order table. – railslearner Apr 07 '11 at 21:29
  • No. Orders.find(ORDER_ID).user retrieved the user object that the order belongs to. – Gazler Apr 07 '11 at 21:33