sql - Queries Ruby on Rails? -


how select in rails?

select women.phone, users.name women, users  (users.cellid = women.cell_id); 

model/user.rb

class user < activerecord::base     attr_accessible :cellid, :name     belongs_to :woman end 

model/woman.rb

class woman < activerecord::base     attr_accessible :cell_id, :phone     has_many :users end 

controller/index.rb

def index   @variables = user.all   @phones = woman.all end 

your select statement implies user , woman linked fields cellid , cell_id, have specify in belongs_to , has_many directives:

class user < activerecord::base   attr_accessible :cellid, :name   belongs_to :woman, foreign_key: :cellid, primary_key: :cell_id end  class woman < activerecord::base   attr_accessible :cell_id, :phone   has_many :users, foreign_key: :cellid, primary_kay: :cell_id end 

while possible, it's better use rails conventions , use :id primary key , woman_id foreign key, if possible.

then in controller can like:

@users= user.all 

and in view:

<% @users.each |user| %>   <%= user.name %> has woman phone <%= user.woman.phone %> <% end %> 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -