ActiveRecord has_one relationship shouldn't this be supported? Happy to make a pull request if yes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

ActiveRecord has_one relationship shouldn't this be supported? Happy to make a pull request if yes

Shishir Kakaraddi
has_one relationships don't have id only getters or setters. For example engineer.laptop_id or engineer.laptop_id= are not implemented in Rails 5. Is there any reason for this?

class Engineer < ActiveRecord::Base
  belongs_to
:team
  has_one
:laptop
  has_many
:tasks
end


engineer
= Engineer.first


# Getters
engineer
.team_id # works
engineer
.laptop_id # doesnt work (Not implemented)
engineer
.task_ids # work


# Same issue with setters too.

Thanks
Shishir

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: ActiveRecord has_one relationship shouldn't this be supported? Happy to make a pull request if yes

James Coleman
No, because belongs_to is for when the foreign key lives on the record with the association and has_one is when the foreign key lives on the associated model instead.

Ypu can't have the _id setter on the model with the association because there is no such field on the model or underlying table. 

On Friday, September 29, 2017, Shishir Kakaraddi <[hidden email]> wrote:
has_one relationships don't have id only getters or setters. For example engineer.laptop_id or engineer.laptop_id= are not implemented in Rails 5. Is there any reason for this?

class Engineer < ActiveRecord::Base
  belongs_to
:team
  has_one
:laptop
  has_many
:tasks
end


engineer
= Engineer.first


# Getters
engineer
.team_id # works
engineer
.laptop_id # doesnt work (Not implemented)
engineer
.task_ids # work


# Same issue with setters too.

Thanks
Shishir

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;rubyonrails-core%2Bunsubscribe@googlegroups.com&#39;);" target="_blank">rubyonrails-core+unsubscribe@googlegroups.com.
To post to this group, send email to <a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;rubyonrails-core@googlegroups.com&#39;);" target="_blank">rubyonrails-core@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: ActiveRecord has_one relationship shouldn't this be supported? Happy to make a pull request if yes

Colin Law
In reply to this post by Shishir Kakaraddi
That is because there is no laptop_id in Engineer (assuming engineer
has_one laptop). Laptop has an engineer_id. You can do
engineer.laptop.id

Colin

On 30 September 2017 at 01:10, Shishir Kakaraddi
<[hidden email]> wrote:

> has_one relationships don't have id only getters or setters. For example
> engineer.laptop_id or engineer.laptop_id= are not implemented in Rails 5. Is
> there any reason for this?
>
> class Engineer < ActiveRecord::Base
>   belongs_to :team
>   has_one :laptop
>   has_many :tasks
> end
>
>
> engineer = Engineer.first
>
>
> # Getters
> engineer.team_id # works
> engineer.laptop_id # doesnt work (Not implemented)
> engineer.task_ids # work
>
>
> # Same issue with setters too.
>
> Thanks
> Shishir
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [hidden email].
> To post to this group, send email to [hidden email].
> Visit this group at https://groups.google.com/group/rubyonrails-core.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: ActiveRecord has_one relationship shouldn't this be supported? Happy to make a pull request if yes

Shishir Kakaraddi
has_many relationship also has the foreign key in another table in the above example it would be in the tasks table. Regardless it has an accessor only for ids of related tasks engineer.task_ids.

Similarly, Shouldn't has_one also have accessors for id even if its in another table?


On Saturday, September 30, 2017 at 6:50:49 AM UTC-7, Colin Law wrote:
That is because there is no laptop_id in Engineer (assuming engineer
has_one laptop). Laptop has an engineer_id. You can do
<a href="http://engineer.laptop.id" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fengineer.laptop.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNG_Q6n-OYfB89p6_H43s3ZatsKIkg&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fengineer.laptop.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNG_Q6n-OYfB89p6_H43s3ZatsKIkg&#39;;return true;">engineer.laptop.id

Colin

On 30 September 2017 at 01:10, Shishir Kakaraddi
<<a href="javascript:" target="_blank" gdf-obfuscated-mailto="EoAN6Bm-BwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">shishirk...@...> wrote:

> has_one relationships don't have id only getters or setters. For example
> engineer.laptop_id or engineer.laptop_id= are not implemented in Rails 5. Is
> there any reason for this?
>
> class Engineer < ActiveRecord::Base
>   belongs_to :team
>   has_one :laptop
>   has_many :tasks
> end
>
>
> engineer = Engineer.first
>
>
> # Getters
> engineer.team_id # works
> engineer.laptop_id # doesnt work (Not implemented)
> engineer.task_ids # work
>
>
> # Same issue with setters too.
>
> Thanks
> Shishir
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="EoAN6Bm-BwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-co...@googlegroups.com.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="EoAN6Bm-BwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
> Visit this group at <a href="https://groups.google.com/group/rubyonrails-core" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;">https://groups.google.com/group/rubyonrails-core.
> For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: ActiveRecord has_one relationship shouldn't this be supported? Happy to make a pull request if yes

Nuno Silva

I think it makes sense. Note that there are a whole other methods that exist on `has_many` relations that do not have equivalent on `has_one`:

- clear
- empty?
- size
- etc (see: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many vs http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_one)

But I think this is intentional... `has_one` associations are tricky and hard to "fit" into a novices mindset.
Having getters like `.association_id` would only make things more complicated when the programmer bangs their heads against the wall because `.association_id = 123`.


On Saturday, September 30, 2017 at 12:29:23 PM UTC-3, Shishir Kakaraddi wrote:
has_many relationship also has the foreign key in another table in the above example it would be in the tasks table. Regardless it has an accessor only for ids of related tasks engineer.task_ids.

Similarly, Shouldn't has_one also have accessors for id even if its in another table?


On Saturday, September 30, 2017 at 6:50:49 AM UTC-7, Colin Law wrote:
That is because there is no laptop_id in Engineer (assuming engineer
has_one laptop). Laptop has an engineer_id. You can do
<a href="http://engineer.laptop.id" rel="nofollow" target="_blank" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fengineer.laptop.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNG_Q6n-OYfB89p6_H43s3ZatsKIkg&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fengineer.laptop.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNG_Q6n-OYfB89p6_H43s3ZatsKIkg&#39;;return true;">engineer.laptop.id

Colin

On 30 September 2017 at 01:10, Shishir Kakaraddi
<[hidden email]> wrote:

> has_one relationships don't have id only getters or setters. For example
> engineer.laptop_id or engineer.laptop_id= are not implemented in Rails 5. Is
> there any reason for this?
>
> class Engineer < ActiveRecord::Base
>   belongs_to :team
>   has_one :laptop
>   has_many :tasks
> end
>
>
> engineer = Engineer.first
>
>
> # Getters
> engineer.team_id # works
> engineer.laptop_id # doesnt work (Not implemented)
> engineer.task_ids # work
>
>
> # Same issue with setters too.
>
> Thanks
> Shishir
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-co...@googlegroups.com.
> To post to this group, send email to [hidden email].
> Visit this group at <a href="https://groups.google.com/group/rubyonrails-core" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;">https://groups.google.com/group/rubyonrails-core.
> For more options, visit <a href="https://groups.google.com/d/optout" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.