Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, from my experience it is generally a bad idea to have associations using the foreign key as name. Especially when writing fixtures it seems rails will get confused between setting the actual value "created_by" or the model in the created_by association. In my models I generally use these associations for the cases you describe:</p> <pre><code>belongs_to :creator, :class_name =&gt; "User", :foreign_key =&gt; 'created_by' belongs_to :modifier, :class_name =&gt; "User", :foreign_key =&gt; 'modified_by' </code></pre> <p>You can use association names like 'creating_user' instead if you prefer. If you really want created_by as association name you should have created_by_id or something similar as foreign key, just as long as its not equal to the association name.</p> <p>Then I am a bit confused by your pasted code. Your choice "Deal has_one User" and "User belongs_to Deal" means that the users table will have the columns created_by and modified_by (foreign keys) containing Deal Ids, basically meaning that users get created by a single deal? However it seems like deals should get created by users and not the other way round. Your example of deal.created_by.email can not work at all with your associations, since deal would not have an association called "created_by", only "user", of which you have two associations with the same name in a single model which can not work at all in the first place.</p> <p>Fixing your associations similar to what Patrick suggested:</p> <pre><code>class Deal &lt; ActiveRecord::Base belongs_to :creator, :class_name =&gt; "User", :foreign_key =&gt; "created_by" belongs_to :modifier, :class_name =&gt; "User", :foreign_key =&gt;"modified_by" end class User &lt; ActiveRecord::Base has_many :created_deals, :class_name =&gt; "Deal", :foreign_key =&gt; "created_by" has_many :modified_deals, :class_name =&gt; "Deal", :foreign_key =&gt; "modified_by" end </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      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