when I use a quart joins on Roles and Profiles models

Role.joins(:profiles).where('profiles.profilable_type' => "Admin")

I get the following generated sql
SELECT `roles`.* FROM `roles` INNER JOIN `profiles_roles` ON
`profiles_roles`.`role_id` = `roles`.`id` INNER JOIN `profiles` ON
`profiles`.`id` = `profiles_roles`.`profile_id` WHERE
`profiles`.`profilable_type` = 'Admin'

which gives me all roles attributes only

I should I write my Rails query to get all attributes ?

SELECT `roles`.*, `profiles`.* FROM `roles` INNER JOIN
`profiles_roles` ON `profiles_roles`.`role_id` = `roles`.`id` INNER
JOIN `profiles` ON `profiles`.`id` = `profiles_roles`.`profile_id`
WHERE `profiles`.`profilable_type` = 'Admin'

in this case, I'll have 2 attributes with same name in both tables
( :id and :name ) is there any way to avoid such collision ( using
AS ...)



======
class Role < ActiveRecord::Base
has_and_belongs_to_many :profiles, :join_table => :profiles_roles

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Colin Law at Jan 16, 2012 at 4:08 pm

    On 16 January 2012 15:54, Erwin wrote:
    when I use   a quart joins on  Roles and Profiles  models

    Role.joins(:profiles).where('profiles.profilable_type' => "Admin")

    I get the following generated sql
    SELECT `roles`.* FROM `roles` INNER JOIN `profiles_roles` ON
    `profiles_roles`.`role_id` = `roles`.`id` INNER JOIN `profiles` ON
    `profiles`.`id` = `profiles_roles`.`profile_id` WHERE
    `profiles`.`profilable_type` = 'Admin'

    which gives me all roles attributes only

    I should I write my Rails query to get all attributes ?

    SELECT `roles`.*, `profiles`.* FROM `roles` INNER JOIN
    `profiles_roles` ON `profiles_roles`.`role_id` = `roles`.`id` INNER
    JOIN `profiles` ON `profiles`.`id` = `profiles_roles`.`profile_id`
    WHERE `profiles`.`profilable_type` = 'Admin'

    in this case, I'll have 2 attributes with same name in both tables
    ( :id  and :name )  is there any way to avoid such collision ( using
    AS ...)



    ======
    class Role < ActiveRecord::Base
    has_and_belongs_to_many :profiles, :join_table => :profiles_roles
    If you get the profiles using something like
    profiles = Profile.where( profileable_type => "Admin")
    then for each profile in the collection you can do profile.roles to
    get all the roles for that profile.

    Alternatively use :includes( :profiles ) rather than join and specify
    a where for the the profilable type which will give you a collection
    of Roles.

    Colin

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Xuan at Jan 17, 2012 at 11:23 am

    On 16 ene, 16:54, Erwin wrote:
    when I use   a quart joins on  Roles and Profiles  models

    Role.joins(:profiles).where('profiles.profilable_type' => "Admin")

    I get the following generated sql
    SELECT `roles`.* FROM `roles` INNER JOIN `profiles_roles` ON
    `profiles_roles`.`role_id` = `roles`.`id` INNER JOIN `profiles` ON
    `profiles`.`id` = `profiles_roles`.`profile_id` WHERE
    `profiles`.`profilable_type` = 'Admin'

    which gives me all roles attributes only

    I should I write my Rails query to get all attributes ?

    SELECT `roles`.*, `profiles`.* FROM `roles` INNER JOIN
    `profiles_roles` ON `profiles_roles`.`role_id` = `roles`.`id` INNER
    JOIN `profiles` ON `profiles`.`id` = `profiles_roles`.`profile_id`
    WHERE `profiles`.`profilable_type` = 'Admin'

    in this case, I'll have 2 attributes with same name in both tables
    ( :id  and :name )  is there any way to avoid such collision ( using
    AS ...)

    ======
    class Role < ActiveRecord::Base
    has_and_belongs_to_many :profiles, :join_table => :profiles_roles
    If for some reason you need the results of the join, you can do:

    Role.joins(:profiles).where('profiles.profilable_type' =>
    "Admin").select('profiles.id, roles.id, profiles.name, profiles.id as
    p_id') # you can use the sql 'as' in the string

    Keep in mind this will return an ActiveRecord::Relation instance, not
    a set of Role or Profile, so if your plan is to iterate through
    results, you better do it as Collin instructed

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJan 16, '12 at 3:54p
activeJan 17, '12 at 11:23a
posts3
users3
websiterubyonrails.org
irc#RubyOnRails

3 users in discussion

Xuan: 1 post Colin Law: 1 post Erwin: 1 post

People

Translate

site design / logo © 2022 Grokbase