开发者

can't do :joins without writing SQL code

I can't understand why this code works:

@ads = Ads.find(
   :all,
   :joins => "INNER JOIN ad_users u ON u.ad_users_id=ads.ad_users_id"
 )

and this one doesn't:

    @ads = Ads.find(
   :all,
   :joins => :AdUsers
 )

my class开发者_开发百科es are:

class Ads < ActiveRecord::Base
  set_primary_key :ads_id
  belongs_to :AdUsers
end

and

class AdUsers < ActiveRecord::Base
  set_primary_key :ad_users_id  
  has_many :Ads
end

I use a sqlite database. The sql generated for the join is:

SELECT "ads".* FROM "ads" INNER JOIN "ad_users" ON "ad_users"."ad_users_id" IS NUL

can anyone help me? I know this is not a blocker, but I don't want to write the join SQL if I don't have to.

thanks


Without digging in too deeply, I see a couple things that could be confusing the join logic. First, I think you want your symbols to be lower cased with underscores on the joins clause and the associations. (Eg, :ad_users instead of :AdUsers) Also, the Rails conventions suggest singular classnames, though if you really want to you can get around this by specifying the class name explicitly:

class AdUsers < ActiveRecord::Base
  set_primary_key :ad_users_id
  has_many :ads, :class_name => 'Ads'
end
# ...and so forth

Unless you have a good reason, though, I'd suggest just using singular class names.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜