How to select with a kind of through relation(has_one)

I have a select issue with my members, my database structure is below

User has many shops
Shop belongs to a membership
Shop has many items

For front page I want to show items where membership of the shop has
ad_enabled and this is what I have in mind but it returns with
“Association named ‘membership’ was not found; perhaps you misspelled
it?”

I tried to add to Item class
belongs_to :membership

And added Membership Class following

has_many :items, :through => :shops

but helaas not working. Any suggestion how to handle this situation?

Thanks in advance

class Item <…

def self.find_by_ad_enabled(options={})
ad_enabled do
self.find :all, {:order => “RAND()”}.merge(options)
end
end

protected
def self.ad_enabled
with_scope(:find => {:conditions => ‘memberships.ad_enabled = 1 and
shops.expires_on >= NOW() and shops.enabled = 1’, :include => [:shop,
:images, :membership]}) do
yield
end
end
end

create_table “items”, :force => true do |t|
t.column “shop_id”, :integer

end

create_table “shops”, :force => true do |t|
t.column “user_id”, :integer, :default => 0, :null => false
t.column “membership_id”, :integer
t.column “expires_on”, :date
t.column “enabled”, :boolean, :default => true

end

create_table “users”, :force => true do |t|

end

create_table “memberships”, :force => true do |t|
t.column “ad_enabled”, :boolean, :default => false

end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs