Rails calling different fields in the same table The Next CEO of Stack OverflowCalling shell commands from RubyWhat's the difference between utf8_general_ci and utf8_unicode_ciA concise explanation of nil v. empty v. blank in Ruby on RailsUnderstanding the Rails Authenticity TokenHow can I rename a database column in a Ruby on Rails migration?How do I get the current absolute URL in Ruby on Rails?How to drop columns using Rails migrationProblems with 2 relationships between 2 tables in the same column under Ruby on RailsWhat's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?Rails Engine: Namespacing foreign key references to other models in the same engine
Horror film about a man brought out of cryogenic suspension without a soul, around 1990
Avoiding the "not like other girls" trope?
Can I cast Thunderwave and be at the center of its bottom face, but not be affected by it?
Prodigo = pro + ago?
Why does freezing point matter when picking cooler ice packs?
Why did the Drakh emissary look so blurred in S04:E11 "Lines of Communication"?
Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?
Create custom note boxes
What did the word "leisure" mean in late 18th Century usage?
Calculating discount not working
Creating a script with console commands
How to implement Comparable so it is consistent with identity-equality
Variance of Monte Carlo integration with importance sampling
Does int main() need a declaration on C++?
Is it okay to majorly distort historical facts while writing a fiction story?
Masking layers by a vector polygon layer in QGIS
Is there a rule of thumb for determining the amount one should accept for of a settlement offer?
Why did Batya get tzaraat?
Is a linearly independent set whose span is dense a Schauder basis?
Another proof that dividing by 0 does not exist -- is it right?
What steps are necessary to read a Modern SSD in Medieval Europe?
What difference does it make matching a word with/without a trailing whitespace?
How does a dynamic QR code work?
Are British MPs missing the point, with these 'Indicative Votes'?
Rails calling different fields in the same table
The Next CEO of Stack OverflowCalling shell commands from RubyWhat's the difference between utf8_general_ci and utf8_unicode_ciA concise explanation of nil v. empty v. blank in Ruby on RailsUnderstanding the Rails Authenticity TokenHow can I rename a database column in a Ruby on Rails migration?How do I get the current absolute URL in Ruby on Rails?How to drop columns using Rails migrationProblems with 2 relationships between 2 tables in the same column under Ruby on RailsWhat's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?Rails Engine: Namespacing foreign key references to other models in the same engine
I want to call different fields in the same table,
Assignedby != Openedby
I cannot call the user to which the topic is assigned
mysql users table
mysql issues table
show.html.erb screenshot
<p>
<strong>Assignedby:</strong>
<%= @issue.user.try(:fullname) %>
</p>
<p>
<strong>Openedby:</strong>
<%= @issue.user.try(:fullname) %>
</p>
class Issue < ApplicationRecord
belongs_to :project, foreign_key: :project_id, optional: true
belongs_to :user, foreign_key: :assignedby_id, optional: true
belongs_to :user, foreign_key: :openedby_id, optional: true
belongs_to :user, foreign_key: :closedby_id, optional: true
end
mysql ruby-on-rails ruby
add a comment |
I want to call different fields in the same table,
Assignedby != Openedby
I cannot call the user to which the topic is assigned
mysql users table
mysql issues table
show.html.erb screenshot
<p>
<strong>Assignedby:</strong>
<%= @issue.user.try(:fullname) %>
</p>
<p>
<strong>Openedby:</strong>
<%= @issue.user.try(:fullname) %>
</p>
class Issue < ApplicationRecord
belongs_to :project, foreign_key: :project_id, optional: true
belongs_to :user, foreign_key: :assignedby_id, optional: true
belongs_to :user, foreign_key: :openedby_id, optional: true
belongs_to :user, foreign_key: :closedby_id, optional: true
end
mysql ruby-on-rails ruby
1
you cannot define several relations with the same name (in your case,user). You should name them likebelongs_to :user_assigner, foreign_key: :assignedby_id, optional: true, class_name: 'User',belongs_to :user_opener, [...]etc etc
– MrYoshiji
Mar 8 at 19:17
@MrYoshiji you are damn fast :)
– Muhammad Faisal Iqbal
Mar 8 at 19:18
@MrYoshiji thank you :)
– Ozgur SARIOGLAN
Mar 8 at 19:21
add a comment |
I want to call different fields in the same table,
Assignedby != Openedby
I cannot call the user to which the topic is assigned
mysql users table
mysql issues table
show.html.erb screenshot
<p>
<strong>Assignedby:</strong>
<%= @issue.user.try(:fullname) %>
</p>
<p>
<strong>Openedby:</strong>
<%= @issue.user.try(:fullname) %>
</p>
class Issue < ApplicationRecord
belongs_to :project, foreign_key: :project_id, optional: true
belongs_to :user, foreign_key: :assignedby_id, optional: true
belongs_to :user, foreign_key: :openedby_id, optional: true
belongs_to :user, foreign_key: :closedby_id, optional: true
end
mysql ruby-on-rails ruby
I want to call different fields in the same table,
Assignedby != Openedby
I cannot call the user to which the topic is assigned
mysql users table
mysql issues table
show.html.erb screenshot
<p>
<strong>Assignedby:</strong>
<%= @issue.user.try(:fullname) %>
</p>
<p>
<strong>Openedby:</strong>
<%= @issue.user.try(:fullname) %>
</p>
class Issue < ApplicationRecord
belongs_to :project, foreign_key: :project_id, optional: true
belongs_to :user, foreign_key: :assignedby_id, optional: true
belongs_to :user, foreign_key: :openedby_id, optional: true
belongs_to :user, foreign_key: :closedby_id, optional: true
end
mysql ruby-on-rails ruby
mysql ruby-on-rails ruby
asked Mar 8 at 19:14
Ozgur SARIOGLANOzgur SARIOGLAN
84
84
1
you cannot define several relations with the same name (in your case,user). You should name them likebelongs_to :user_assigner, foreign_key: :assignedby_id, optional: true, class_name: 'User',belongs_to :user_opener, [...]etc etc
– MrYoshiji
Mar 8 at 19:17
@MrYoshiji you are damn fast :)
– Muhammad Faisal Iqbal
Mar 8 at 19:18
@MrYoshiji thank you :)
– Ozgur SARIOGLAN
Mar 8 at 19:21
add a comment |
1
you cannot define several relations with the same name (in your case,user). You should name them likebelongs_to :user_assigner, foreign_key: :assignedby_id, optional: true, class_name: 'User',belongs_to :user_opener, [...]etc etc
– MrYoshiji
Mar 8 at 19:17
@MrYoshiji you are damn fast :)
– Muhammad Faisal Iqbal
Mar 8 at 19:18
@MrYoshiji thank you :)
– Ozgur SARIOGLAN
Mar 8 at 19:21
1
1
you cannot define several relations with the same name (in your case,
user). You should name them like belongs_to :user_assigner, foreign_key: :assignedby_id, optional: true, class_name: 'User', belongs_to :user_opener, [...] etc etc– MrYoshiji
Mar 8 at 19:17
you cannot define several relations with the same name (in your case,
user). You should name them like belongs_to :user_assigner, foreign_key: :assignedby_id, optional: true, class_name: 'User', belongs_to :user_opener, [...] etc etc– MrYoshiji
Mar 8 at 19:17
@MrYoshiji you are damn fast :)
– Muhammad Faisal Iqbal
Mar 8 at 19:18
@MrYoshiji you are damn fast :)
– Muhammad Faisal Iqbal
Mar 8 at 19:18
@MrYoshiji thank you :)
– Ozgur SARIOGLAN
Mar 8 at 19:21
@MrYoshiji thank you :)
– Ozgur SARIOGLAN
Mar 8 at 19:21
add a comment |
1 Answer
1
active
oldest
votes
You cannot give all these belongs_to associations the same name, they need to be different, for example:
class Issue < ApplicationRecord
with_option optional: true do
belongs_to :project
belongs_to :assigner, class_name: 'User', foreign_key: :assignedby_id
belongs_to :creator, class_name: 'User', foreign_key: :openedby_id
belongs_to :closer, class_name: 'User', foreign_key: :closedby_id
end
end
assigned_useris contradictory with the foreign key, it seems the relation here is to point to the user who assigned it, not who is assigned to it
– MrYoshiji
Mar 8 at 19:28
add a comment |
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55069593%2frails-calling-different-fields-in-the-same-table%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You cannot give all these belongs_to associations the same name, they need to be different, for example:
class Issue < ApplicationRecord
with_option optional: true do
belongs_to :project
belongs_to :assigner, class_name: 'User', foreign_key: :assignedby_id
belongs_to :creator, class_name: 'User', foreign_key: :openedby_id
belongs_to :closer, class_name: 'User', foreign_key: :closedby_id
end
end
assigned_useris contradictory with the foreign key, it seems the relation here is to point to the user who assigned it, not who is assigned to it
– MrYoshiji
Mar 8 at 19:28
add a comment |
You cannot give all these belongs_to associations the same name, they need to be different, for example:
class Issue < ApplicationRecord
with_option optional: true do
belongs_to :project
belongs_to :assigner, class_name: 'User', foreign_key: :assignedby_id
belongs_to :creator, class_name: 'User', foreign_key: :openedby_id
belongs_to :closer, class_name: 'User', foreign_key: :closedby_id
end
end
assigned_useris contradictory with the foreign key, it seems the relation here is to point to the user who assigned it, not who is assigned to it
– MrYoshiji
Mar 8 at 19:28
add a comment |
You cannot give all these belongs_to associations the same name, they need to be different, for example:
class Issue < ApplicationRecord
with_option optional: true do
belongs_to :project
belongs_to :assigner, class_name: 'User', foreign_key: :assignedby_id
belongs_to :creator, class_name: 'User', foreign_key: :openedby_id
belongs_to :closer, class_name: 'User', foreign_key: :closedby_id
end
end
You cannot give all these belongs_to associations the same name, they need to be different, for example:
class Issue < ApplicationRecord
with_option optional: true do
belongs_to :project
belongs_to :assigner, class_name: 'User', foreign_key: :assignedby_id
belongs_to :creator, class_name: 'User', foreign_key: :openedby_id
belongs_to :closer, class_name: 'User', foreign_key: :closedby_id
end
end
edited Mar 8 at 19:34
answered Mar 8 at 19:20
spickermannspickermann
61.4k75880
61.4k75880
assigned_useris contradictory with the foreign key, it seems the relation here is to point to the user who assigned it, not who is assigned to it
– MrYoshiji
Mar 8 at 19:28
add a comment |
assigned_useris contradictory with the foreign key, it seems the relation here is to point to the user who assigned it, not who is assigned to it
– MrYoshiji
Mar 8 at 19:28
assigned_user is contradictory with the foreign key, it seems the relation here is to point to the user who assigned it, not who is assigned to it– MrYoshiji
Mar 8 at 19:28
assigned_user is contradictory with the foreign key, it seems the relation here is to point to the user who assigned it, not who is assigned to it– MrYoshiji
Mar 8 at 19:28
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55069593%2frails-calling-different-fields-in-the-same-table%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown

1
you cannot define several relations with the same name (in your case,
user). You should name them likebelongs_to :user_assigner, foreign_key: :assignedby_id, optional: true, class_name: 'User',belongs_to :user_opener, [...]etc etc– MrYoshiji
Mar 8 at 19:17
@MrYoshiji you are damn fast :)
– Muhammad Faisal Iqbal
Mar 8 at 19:18
@MrYoshiji thank you :)
– Ozgur SARIOGLAN
Mar 8 at 19:21