Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I resolved the issue on my own.. Before posting answer I must say everyone should user rails console</p> <p>I have define relations as follows</p> <pre><code>class User &lt; AR::Base has_one :item_preference has_one :item_preference, :through =&gt; :user_preference, :source =&gt; :item has_one :favorite_preference has_one :favorite_preference, :through =&gt; :user_preference, :source =&gt; :favorite has_one :recent_preference has_one :recent_preference, :through =&gt; :user_preference, :source =&gt; :recent has_one :user_preference end class UserPreference &lt; ActiveRecord::Base belongs_to :item, :foreign_key =&gt; "item_results", :class_name =&gt; "Preference" belongs_to :favorite, :foreign_key =&gt; "favorite_results", :class_name =&gt; "Preference" belongs_to :recent, :foreign_key =&gt; "recent_results", :class_name =&gt; "Preference" belongs_to :user end class Preference &lt; ActiveRecord::Base has_many :item_preferences, :foreign_key =&gt; "item_results", :class_name =&gt; "UserPreference" has_many :favorite_preferences, :foreign_key =&gt; "favorite_results", :class_name =&gt; "UserPreference" has_many :recent_preferences, :foreign_key =&gt; "recent_results", :class_name =&gt; "UserPreference" end </code></pre> <p>now I can access the values as</p> <pre><code>User = User.find(16) user.item_preference [:] [DEBUG] [2010-09-16 17:09:54] [ App::OnlineOrdering] - Preference Load (0.4ms) SELECT `preferences`.* FROM `preferences` INNER JOIN `user_preferences` ON `preferences`.id = `user_preferences`.item_results WHERE ((`user_preferences`.user_id = 16)) =&gt; #&lt;Preference id: 1, value: "20", created_at: "2010-09-15 15:13:04", updated_at: "2010-09-15 15:13:04"&gt; </code></pre> <p>and other way around</p> <pre><code>preference = Preference.find(1) preference.item_preferences [:] [DEBUG] [2010-09-16 17:09:23] [ App::OnlineOrdering] - UserPreference Load (0.4ms) SELECT * FROM `user_preferences` WHERE (`user_preferences`.item_results = 1) [:] [DEBUG] [2010-09-16 17:09:23] [ App::OnlineOrdering] - UserPreference Columns (1.2ms) SHOW FIELDS FROM `user_preferences` =&gt; [#&lt;UserPreference id: 1, user_id: 16, item_results: 1, favorite_results: 2, recent_results: 2, created_at: "2010-09-15 15:13:04", updated_at: "2010-09-15 15:13:04"&gt;] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload