Ruby on Rails - primary and foreign key

I am creating a site in Ruby on Rails, I have two models: model User

and model Transaction

.

These models are account specific, so they have a field called account_id

I am trying to communicate between them like this:

class User < ActiveRecord::Base
  belongs_to :account
  has_many :transactions
end

class Transaction < ActiveRecord::Base
  belongs_to :account
  belongs_to :user
end

      

I use the following associations:

user = User.find(1)
transactions = user.transactions

      

The application is currently trying to find transactions from 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 not correct, since I would like to find transactions through account_id

, I tried setting associations like this:

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'm looking for 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 correct account_id

, but it is still trying to query the transaction table for the field user_id

. Can anyone give me some advice on how to set up associations to query the transaction table for account_id

instead user_id

, resulting in an SQL query:

SELECT * FROM `transactions` WHERE (`transactions`.account_id = 104)

      

Greetings

Eef

+2


a source to share


3 answers


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

+3


a source


If transactions

there is no column in the table user_id

, then you should use the model Account

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 need to remove has_many :transactions

from User

and belongs_to :user

from Transaction

as they assume you have a column user_id

.

0


a source


I want to store data in an address model that is related to the 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

      

-1


a source







All Articles