0

I'm new to rails and testing and have a problem with loading my fixtures.

payment.yml
payment:
  name: something
  amount: 1.5
  event: some_event
  user: some_user
  description: long text
  users: some_user, some_user2


users.yml
some_user:
  email: test@test.com   nick: name

some_user2:
  email: test@test.com
  nick: name

okey, so the problem is that when I'm doing the functionaltest for creating a payment

test "should create payment" do
assert_difference('Payment.count') do
  post :create, :payment => @payment.attributes
end

it just sends

< Payment id: nil, name: "something", amount: 1.5, event_id: 972288058, user_id: 63246679, created_at: "2010-11-05 19:56:53", updated_at: "2010-11-05 19:56:53", description: "long text" >

and not the users array with multiple users. I use the "user" to define who owns the payment and users (in a seperate join table) as a list of users sharing the payment.

Any idea what I'm doing wrong?

here is the model for payments

class Payment < ActiveRecord::Base
  has_many :comments, :as => :commentable, :dependent => :destroy
  belongs_to :event
  belongs_to :user
  has_and_belongs_to_many :users
end

jonepatr
  • 7,769
  • 7
  • 30
  • 53

1 Answers1

1

Can you put the model for payment? I had something similar and the problem was that the model was different. For example, if it's a ActiveRecord class, you have to check how it and its relationships. For example, if the event is actually an "event_id" that belongs_to an "event" class, then you should put something similar to this:

payment.yml
payment:
  name: something
  amount: 1.5
  event_id: 1
  user_id: 1
  ...
user.yml
   id: 1
   name: David Smith
   status: Branch Manager
  ...
event.yml
  id: 1
  name: overdraft charge
marcelo
  • 193
  • 1
  • 8
  • but how about that I'm having one user that is "belongs_to" and one users that is "has_and_belongs_to_many"? – jonepatr Nov 06 '10 at 00:58
  • Ruby does that, but you still have to handle it in the fixture yml file. In my example, lets say I have a relationship of one-to-one with payment and user. Also, I have a one-to-one with payment and event. For example, in my payment model I would have something like this: belongs_to : user, :foreign_key => "user_id". Again, this is in the model portion. Check this link: http://api.rubyonrails.org/classes/Fixtures.html and look for "Label references for associations (belongs_to, has_one, has_many)" – marcelo Nov 08 '10 at 18:31