What's the difference between belongs_to and has_one?
They essentially do the same thing, the only difference is what side of the relationship you are on. If a User
has a Profile
, then in the User
class you'd have has_one :profile
and in the Profile
class you'd have belongs_to :user
. To determine who "has" the other object, look at where the foreign key is. We can say that a User
"has" a Profile
because the profiles
table has a user_id
column. If there was a column called profile_id
on the users
table, however, we would say that a Profile
has a User
, and the belongs_to/has_one locations would be swapped.
here is a more detailed explanation.
Difference between has_one and belongs_to in Rails?
No, they are not interchangable, and there are some real differences.
belongs_to
means that the foreign key is in the table for this class. So belongs_to
can ONLY go in the class that holds the foreign key.
has_one
means that there is a foreign key in another table that references this class. So has_one
can ONLY go in a class that is referenced by a column in another table.
So this is wrong:
class Person < ActiveRecord::Base
has_one :cell # the cell table has a person_id
end
class Cell < ActiveRecord::Base
has_one :person # the person table has a cell_id
end
And this is also wrong:
class Person < ActiveRecord::Base
belongs_to :cell # the person table has a cell_id
end
class Cell < ActiveRecord::Base
belongs_to :person # the cell table has a person_id
end
The correct way is (if Cell
contains person_id
field):
class Person < ActiveRecord::Base
has_one :cell # the person table does not have 'joining' info
end
class Cell < ActiveRecord::Base
belongs_to :person # the cell table has a person_id
end
For a two-way association, you need one of each, and they have to go in the right class. Even for a one-way association, it matters which one you use.
What is the difference between has_one and belongs_to in active record associations?
The difference is in where the foreign key is stored.
So for example if a post
belongs to a user
, the post table will have a column user_id
so the post knows which user it belongs to.
has_one
only makes methods like some_user.post available.
See also: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Is+it+a+belongs_to+or+has_one+association%3F
Should I use has_one or belongs_to in ruby on rails?
There is a guide on this very question. Your situation is slightly different in that it seems as though your Status model really needs to be polymorphic since different things will be 'statusable'.
To answer your question, Contact/Event has_one Status does make sense to me.
Differences between has_one and belongs_to in Rails
The belongs to is on the table with the foreign key. For the following:
class User < ActiveRecord::Base
has_one :profile
end
class Profile < ActiveRecord::Base
belongs_to :user
end
The profiles table needs to have a user_id field to reference the record in the users table.
Knowing which to be belongs_to and which to be has_one is something many people struggle with. Generally, if the has_one might likely become a has_many, then that's the side which needs to be has_one.
Rails nested includes and belongs_to vs. has_one
For the following associations:
# User
has_many :jobs, dependent: :destroy
has_many :job_requests
# Job
belongs_to :user
has_many :job_requests
# JobRequest
belongs_to :user
belongs_to :job
Your query will be:
current_user.jobs.includes(job_requests: :user).find(params[:id])
belongs_to
& has_one
:
The only difference is what side of the relationship you are on. If a User
has a Profile
, then in the User
model you need to have has_one :profile
and in the Profile
model you need to have belongs_to :user
.
You can determine who has the other object by looking at where the foreign key is. You can say a User
has a Profile
because the profiles
table has a user_id
column. If there is a column called profile_id
on the users
table, we will say that a Profile
has a User
.
For further study you can read this article.
Difference between has_one and belongs_to on a 1 to many relationship in Ruby on Rails
It won't actually be the same result...
The first one:
class Customer
belongs_to :user
end
class User
has_many :customers
end
Will set a user_id in the Customer and nothing in the User. That means that a customer can only be related to one user. In terms of reflections you can do stuff like this:
user = User.create(name: 'John Snow')
customer = user.customers.build(name: 'Tywin Lannister')
customer.save
user.inspect
=> #<User id: 8, name: "John Snow">
customer.inspect
=> #<Customer id: 12, user_id: 8, name: "Tywin Lannister">
user.customers.inspect
=> [#<Customer id: 12, user_id: 8, name: "Tywin Lannister">]
customer.user
=> #<User id: 8, name: "John Snow">
The second one:
class Customer
has_one :user
end
class User
belongs_to :customer
end
Will set a customer_id in the user. You can do stuff like this:
customer = Customer.create(name: 'Tywin Lannister')
user = customer.build_user(name: 'John Snow')
user.inspect
=> #<User id: 8, customer_id: 12, name: "John Snow">
customer.inspect
=> #<Customer id: 12, name: "Tywin Lannister">
user.customer
=> #<Customer id: 12, name: "Tywin Lannister">
customer.user
=> #<User id: 8, customer_id: 12, name: "John Snow">
So in your case
Well you need the first one.
From the documentation:
A belongs_to association sets up a one-to-one connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model.
A has_one association also sets up a one-to-one connection with another model, but with somewhat different semantics (and consequences). This association indicates that each instance of a model contains or possesses one instance of another model.
Related Topics
How to Return Early from a Rake Task
"Which in Ruby": Checking If Program Exists in $Path from Ruby
How to Merge Two Hashes Without Overwritten Duplicate Keys in Ruby
Create Module Variables in Ruby
Installing Cocoapods: No Response
How to Suppress Rails Console/Irb Outputs
Docker for MAC - Mkmf.Rb Can't Find Header Files for Ruby
Understanding the Gemfile.Lock File
How to Sort a Ruby Hash by Number Value
How to Get Sinatra to Auto-Reload the File After Each Change
Difference Between Has_One and Belongs_To in Rails
Any Success with Sinatra Working Together with Eventmachine Websockets
How to Pass Parameter on 'Vagrant Up' and Have It in the Scope of Vagrantfile
Ruby: How to Iterate Over a Range, But in Set Increments