I am creating a site in Ruby on Rails, I have two models a User
model开发者_如何学编程 and a Transaction
model.
These models both belong to an account so they both have a field called account_id
I am trying to setup a association between them like so:
class User < ActiveRecord::Base
belongs_to :account
has_many :transactions
end
class Transaction < ActiveRecord::Base
belongs_to :account
belongs_to :user
end
I am using these associations like so:
user = User.find(1)
transactions = user.transactions
At the moment the application is trying to find the transactions with the user_id
, here is the SQL it generates:
Mysql::Error: Unknown column 'transactions.user_id' in 'where clause': SELECT * FROM `transactions` WHERE (`transactions`.user_id = 1)
This is incorrect as I would like the find the transactions via the account_id
, I have tried setting the associations like so:
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
This almost achieves what I am looking to do and generates the following SQL:
Mysql::Error: Unknown column 'transactions.user_id' in 'where clause': SELECT * FROM `transactions` WHERE (`transactions`.user_id = 104)
The number 104
is the correct account_id
but it is still trying to query the transaction table for a user_id
field. Could someone give me some advice on how I setup the associations to query the transaction table for the account_id
instead of the user_id
resulting in a SQL query like so:
SELECT * FROM `transactions` WHERE (`transactions`.account_id = 104)
Cheers
Eef
class Account < ActiveRecord::Base
has_many :users
has_many :transactions
end
class User < ActiveRecord::Base
has_many :transactions, :through => :account
end
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001833
If you don't have the columnt user_id
in the transactions
table then you should use the Account
model to select all transactions:
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
Note that you have to remove has_many :transactions
from User
and belongs_to :user
from Transaction
as they assume that you do have the user_id
column.
I want to store data in address model that association with student model
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
end
精彩评论