我正在用Ruby on Rails创建一个站点,我有两个模型,一个是User模型,一个是Transaction模型。
这两个模型都属于一个帐户,因此它们都有一个名为account_id的字段
我尝试在它们之间建立一个关联,如下所示:
class User < ActiveRecord::Base
belongs_to :account
has_many :transactions
end
class Transaction < ActiveRecord::Base
belongs_to :account
belongs_to :user
end我像这样使用这些关联:
user = User.find(1)
transactions = user.transactions此时,应用程序正在尝试使用user_id查找事务,下面是它生成的SQL:
Mysql::Error: Unknown column 'transactions.user_id' in 'where clause': SELECT * FROM `transactions` WHERE (`transactions`.user_id = 1)这是不正确的,因为我希望通过account_id查找交易,我已经尝试像这样设置关联:
class User < ActiveRecord::Base
belongs_to :account
has_many :transactions, :primary_key => :account_id, :class_name => "Transaction"
end
class Transaction < ActiveRecord::Base
belongs_to :account
belongs_to :user, :foreign_key => :account_id, :class_name => "User"
end这几乎实现了我想要做的事情,并生成了以下SQL:
Mysql::Error: Unknown column 'transactions.user_id' in 'where clause': SELECT * FROM `transactions` WHERE (`transactions`.user_id = 104)编号104是正确的account_id,但它仍在尝试查询事务表中的user_id字段。谁能给我一些建议,关于我如何设置关联来查询transaction表中的account_id,而不是导致如下SQL查询的user_id:
SELECT * FROM `transactions` WHERE (`transactions`.account_id = 104)干杯
Eef
发布于 2010-05-10 18:36:29
class Account < ActiveRecord::Base
has_many :users
has_many :transactions
end
class User < ActiveRecord::Base
has_many :transactions, :through => :account
endhttp://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001833
发布于 2010-05-10 18:39:27
如果transactions表中没有列user_id,那么应该使用Account模型来选择所有事务:
class User < ActiveRecord::Base
belongs_to :account
end
class Account < ActiveRecord::Base
has_many :transactions
has_one :user # or has_many :users
end
class Transaction < ActiveRecord::Base
belongs_to :account
end
User.find(1).account.transactions请注意,您必须从User中删除has_many :transactions,从Transaction中删除belongs_to :user,因为它们假定您确实有user_id列。
发布于 2015-03-20 13:12:20
我想将数据存储在与学生模型相关联的地址模型中
class AddressesController < ApplicationController
def new
@address = Address.new
end
def create
@address = Address.create(:address_date => Time.now,
:student_id => @student.id)
end
private
def address_params
params.require(:address).permit(:gali_no, :house_no_flate_no, :vill_town_city, :district, :state, :post_code, :country)
end
end
class StudentsController < ApplicationController
def show
end
def new
@student = Student.new
end
def create
@student = Student.new(student_params)
if @student.save
redirect_to root_url, :notice => "You have been registered"
else
render "new"
end
end
private
def student_params
params.require(:student).permit(:f_name, :l_name, :email, :password, :password_confirmation, :mobile_no)
end
end
class Student < ActiveRecord::Base
has_many :addressess, :dependent => :destroy
attr_accessor :password
before_save :encrypt_password
validates_confirmation_of :password
validates_presence_of :f_name
validates_presence_of :l_name
validates_presence_of :password, :on => :create
validates_presence_of :email
validates_uniqueness_of :email
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
end
class Address < ActiveRecord::Base
belongs_to :student
endhttps://stackoverflow.com/questions/2801900
复制相似问题