ruby on rails - translating a sql query to active records -


i've got 2 models below. secondant role user can play user, , secondant_id pointing user.id. want have users current_user plays role of secondant. can write query in sql best way translate active records?

class secondant < activerecord::base    belongs_to :user   before_save :find_user end  class user < activerecord::base    devise :database_authenticatable, :registerable,          :recoverable, :rememberable, :trackable, :validatable, :confirmable    has_many :secondants end 

the query gives me result want , want convert active record is:

select     *     users,     secondants     users.id = secondants.secondant_id ,     secondant_id = 2; 

below query might work(untested)

user.select("users.*, secondants.*").joins("join secondants on users.id = secondants.secondant_id").where("secondants.secondant_id = ?", 2) 

you can't access attributes secondants directly on users. have access like, user["attribute_name"]


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -