简体   繁体   中英

'join' not woking in ruby on rails

In my users model:

id | name | user_Zip
1  | abc  | 10005
2  | mno  | 10005
3  | ijk  | 10005

And, In my professions model:

id | UserID | Designation
1  | 1      | Lead
2  | 2      | Lead
3  | 3      | Software Engineer

UserID is the foreign_key. In my search text-box I am enter "user_Zip", ie 10005, then I want it to give me a name and a designation of users whose user_Zip = 10005 .

I am using this code:

def search
    if ( !params[:tf_Zip].blank? or params[:tf_Zip] != "" )
       @user_zip = User.find(:all,:conditions=>['"user_Zip" = ? ',params[:tf_Zip]])
        end
end

This works fine, but it doess not give me the designation of the user.

When I use this code with join:

def search
  if ( !params[:tf_Zip].blank? or params[:tf_Zip] != "" )
    @user_zip = Profession.joins(:user).where('users.user_Zip' => params[:tf_Zip]) 
  end
end

It also works fine, but does not give me the required result.

These are the models:

# user's model
class User < ActiveRecord::Base
  has_many :professions, dependent: :destroy
end

# profession's model
class Profession < ActiveRecord::Base
    belongs_to :user, foreign_key: :UserID
    validates :UserID, presence: true
end

As you can see above, I set foreign_key UserID but it gave me "progession"."user_id" = ? and it makes by default foreign_key user_id

I would do this:

Schemas

#users
id | name | profession_id | zip
1  | abc  | 1             | 10005
2  | mno  | 1             | 10005
3  | ijk  | 2             | 10005

#professions
id | designation
1  | Lead
2  | Software Engineer

Models

#app/models/user.rb
Class User < ActiveRecord::Base
    belongs_to :profession
end

#app/models/profession.rb
Class Profession < ActiveRecord::Base
    has_many :users
end

Actions

def search
    zip = params[:tf_Zip]
    unless zip && zip.blank?
       @users = User.where(zip: zip) #-> returns User records for SQL
    end
end

@users.each do |user|
    user.profession.designation #-> outputs "Lead" / "Software Engineer" for each user
end

I would like to add more on answer given by Rich

Models

#app/models/user.rb
class User < ActiveRecord::Base
  belongs_to :profession

  def self.search keyword
    return [] if keyword.blank?
    self.includes(:profession).where(zip: keyword.strip)
  end
end

#app/models/profession.rb
class Profession < ActiveRecord::Base
    has_many :users
end

Action

def search
    @users = User.search params[:tf_Zip]
end

View

@users.each do |user|
    user.profession.designation
end

In this way it will fire only two queries.

------------------- OR -------------------

if you strictly wants join(1 single query) then you can write it as follows

Models

#app/models/user.rb
Class User < ActiveRecord::Base
  belongs_to :profession

  def self.search keyword
    return [] if keyword.blank?
    self.join(:profession).where(zip: keyword.strip).select("users.*, professions.name")
  end
end

#app/models/profession.rb
Class Profession < ActiveRecord::Base
    has_many :users
end

Action

def search
    @users = User.search params[:tf_Zip]
end

View

@users.each do |user|
    user['designation']
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM