我正在使用以下模型函数构建应用程序
到目前为止,我已将模型设置如下:
# user.rb
class User < ActiveRecord::Base
attr_accessible :first_name, :last_name, :email, :password
has_many :memberships, :foreign_key => "member_id", :dependent => :destroy
has_many :groups, :through => :memberships
has_many :owings
end
# group.rb
class Group < ActiveRecord::Base
attr_accessible :name
has_many :memberships, :dependent => :destroy
has_many :members, :through => :memberships
has_many :expenses
end
# expense.rb
class Expense < ActiveRecord::Base
attr_accessible :total_dollars, :name, :owings_attributes, :added_by_user_id
belongs_to :group, :inverse_of => :expense
has_many :owings, :dependent => :destroy
end
# owing.rb
class Owing < ActiveRecord::Base
attr_accessible :amount_dollars, :user_id
belongs_to :expense, :inverse_of => :owings
belongs_to :user, :inverse_of => :owings
end
# NB - have left off memberships class (and some attributes) for simplicity
要创建费用,我使用@ group.expenses.build(params [:expenses]),其中params来自嵌套模型表单,其中包含需要创建的所有权的属性。参数包括该费用的每个'欠款'实例的'user_id'。
我有两个问题:
有什么想法吗?很可能我在这里遗漏了一些相当基本的东西。
提前致谢!
PS。长时间听众,第一次来电。感谢大家教我迄今为止在轨道上的红宝石;这个网站是一个令人难以置信的资源!