English 中文(简体)
HABTM Relationship and Named Scopes
原标题:

I ve been working on this for a while now and I cant seem to find where there error.

I have a User Model(:name, :password, :email), and Event model(:name, :etc) and Interest model (:name) [>all singular<]

Then I created two join tables -> UsersInterests and EventsInterests; each not containing a primary key and only comprised of the user_id/interest_id and event_id/interest_id respectively. [>plural<]

My Models contain the HABTM Relationships as follows

user.rb => has_and_belongs_to_many :interests
event.rb => has_and_belongs_to_many :interests
interest.rb => has_and_belongs_to_many :users
               has_and_belongs_to_many :events
events_interests.rb => has_and_belongs_to_many :interests
                       has_and_belongs_to_many :events
users_interests.rb => has_and_belongs_to_many :users
                      has_and_belongs_to_many :interests

Whew..ok So I wanted to created a named_scope of that find all the events that share interest with a particular user. Here is some code someone helped me with.

named_scope :shares_interest_with_user, lambda {|user| { :joins => "LEFT JOIN
              events_interests ei ON ei.event_id = Event.id " +
             "LEFT JOIN users_interests ui ON ui.interest_id = ei.interest_id",
           :conditions => ["ui.user_id = ?", user]  }}

When i run from the controller =>

  @user = User.find(1) 
  @events = Event.shares_interest_with_user(@user)

I get the SQL error : "no such column: Event.id: SELECT "events".* FROM "events" LEFT JOIN events_interests ei ON ei.event_id =..........."

Now I m sure the event model has an id and I think I have all the column names right..can anyone see what is missing?

最佳回答

I suggest moving away from HABTM and instead using has_many :through.

Here s a great blog post explaining the difference written by SO user Jaime Bellmyer.

问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签