My user.location is returning nil currently.
My model looks like:
# id
# user_location_id
class User < ApplicationRecord
belongs_to :location, class_name: "UserLocation"
end
user = User.find(1)
user.location.id # returns nil
Do I have to tell me model how to find the association in the UserLocation model?
Be sure you have user_location_id as foreign_key to user table.
You can add it to you association.
belongs_to :location, class_name: "UserLocation", foreign_key: "user_location_id"
I hope this help you.
Related
Given the following polymorphic relationship:
class Note < ApplicationRecord
belongs_to :noteable, polymorphic: true
has_one :garden, foreign_key: :id, primary_key: :noteable_id
end
class Garden < ApplicationRecord
has_many :notes, as: :noteable
end
I'd like to verify that noteable_type is Garden to prevent mismatches.
What is a good implementation for this problem?
You can use the build_in is_a? method to check what the associated record is. So basically you can check it is a garden by doing:
Note.find(1).garden.is_a? Garden
If this is true, you are sure it is indeed a garden.
Just a tip: I'd change the name of the has_one, because this already assumes it is a garden, while it can be something else in a polymorphic relationship.
Rails novice here. I have associated models Team and Venue defined as follows:
class Team < ApplicationRecord
has_many :home_venues, class_name: "Venue"
end
and
class Venue < ApplicationRecord
belongs_to :team, optional: true, foreign_key: :team_id
end
Both models have attributes :city and :region. When I call team.home_venues.create, I would like for the :city and :region values of newly-created venue to default to the :city and :region values of the creating team unless otherwise specified.
What's the best way to achieve this functionality?
I would use before_validation hook - this way you will make sure you will have all validations run at correct time. In your Venue model:
before_validation :set_default_values
def set_default_values
self.city ||= self.team.try(:city)
self.region ||= self.team.try(:region)
end
I am new to rails and this is a very basic question. I am trying to understand the need of foreign key and class_name.
has_many :task, foreign_key: "created_by"
has_many :memberships, class_name: "TaskMembership"
Can anyone explain the need of foreign_key & class_name.
Here is the answer of my question
Suppose you have a User model and Post model.And you have to set an association like User has many post
User Model
has_many :posts
Post Model
belongs_to :user
Now suppose your user is some author so we have to set some meaningful name so instead of user we will use author but have to specify which class it is referring
Post Model
belongs_to :author, class_name: 'User'
Now problem will occur because rails will look for author_id column in posts table .So here foreign key will come into picture.We will have to find user_id
Post Model
belongs_to :author, class_name: 'User', foreign_key: 'user_id'
See more better explanation association
has_many association is used for for one-to-many type relationships in rails. For instance, if you have a model User which can has many profiles, your User to Profile association will be has many.
class User < ActiveRecord::Base
has_many :profiles
end
class Profile < ActiveRecord::Base
belongs_to :user
end
If you have a foreign key different than user_id in profiles table, you explicitly specify foreign_key. Same is the case with class name. If your association name is different than actual model name, you explicitly specify class name after association (as you did for memberships).
Hope it helps.
in your model
class First < ActiveRecord::Base
has_many :seconds
end
class Second < ActiveRecord::Base
belongs_to :first
end
and in your second class table,create first_id column
I had this setup in my app:
User has_many :products
User has_many :product_leads, through: :products
Product belongs_to :user # (in products table there is user_id)
ProductLead belongs_to :product # (in product_leads table there is no user_id)
ProductLead belongs_to :user
With this setup user.product_leads is working but product_lead.user is not. So I deleted the last line.
I don't have user_id in the product_leads table, but I don't even want to. ProductLead form is filled as nested attribute together with Product form.
Is there a better way to define product_lead.user than below? Or should I rather have user_id in the product_leads table?
product_lead.rb
def user
product.user
end
I believe the has_one :through association would help you.
def ProductLead < ActiveRecord::Base
has_one :product
has_one :user, through: :product
# ... other code
end
I don't see any problem with the approach you've proposed as long as it's read-only you don't need to assign a user to a product.
You should, however, guard against calling user on nil in the case product doesn't exist:
def user
product.try(:user) # Only call `user` if product is non-nil.
end
I've got a polymorphic association with a has_one and it gives me an error when trying to create through association.
class User < ActiveRecord::Base
belongs_to :userable, polymorphic: true
end
class Student < ActiveRecord::Base
attr_accessible :gender, :description, :dob
has_one :user, :as => :userable
end
If I try to do:
s = Student.new
s.user.create
I get and error Undefined method create for 'nil'
But! If i change the association to has_many users then I can now preform the same lines above.
Can anyone explain why this is happening? Thanks!
The problem is that user is nil since you haven't assigned a value to it.
You should use something like:
s.build_user(...)
or
s.create_user(...)