I want my user object to be able to be associated with many addresses, and for one of those addresses to be the primary address.
I'm trying to do this without using a Boolean to denote the primary address, instead using both a has-many and and a has-one association - as per the first approach by PinnyM in the following SO: Rails model that has both 'has_one' and 'has_many' but with some contraints
But I can't seem to get it to work.
My migrations:
class User < ActiveRecord::Migration
def change
create_table(:users) do |t|
t.integer :primary_address_id
t.string :name
end
end
end
class Address < ActiveRecord::Migration
def change
create_table(:addresses) do |t|
t.integer :user_id
t.string :address
end
end
end
My models:
class User
has_many :addresses
has_one :primary_address, :class_name => "Address"
end
class Address
belongs_to :user
has_one :user
end
This allows me to use the has_many association by doing user.addresses but I can't seem to access to has one association. I've tried doing:
user.primary_address
user.addresses.primary_address
user.addresses.primary_address.first
I don't really understand how to set these associations up correctly or how to access them. Would appreciate your help!
Just created models and associations that you are using. I don't see why it is not working in your case, because I can access primary_address. This is the code I am using to access it using rails console. Note: I have created a User and couple of Addresses in advance.
# in case if you have user with id = 1
User.find(1).primary_address
# or another example
User.first.primary_address
I don't think your associations will allow this calls though:
user.addresses.primary_address
user.addresses.primary_address.first
Related
All of the references I've found either show me how to do it upon table creation, or are for a much earlier version of rails. Ideally, I'd like like the foreign_key to be named 'author_id' in the questions table to distinguish it from other users who may be leaving comments or answers later.
class Question < ApplicationRecord
belongs_to :user
end
class User < ApplicationRecord
has_many :questions
end
You can create a new empty migration file in your terminal via rails generate migration RenameUserFkOnQuestion. Open it up and build your migration. This is a handy guide if you're not sure on the name of something.
def change
change_table :questions do |t|
t.rename :user_id, :author_id
end
end
Run the migration and head over to your models. You'll need to update your relationships like so:
class Question
belongs_to :author, class_name: 'User'
end
class User
has_many :questions, inverse_of: :author
end
Then you should be good to go.
I created a has_and_belongs_to_many association between users and projects so that a user would be able to join in on a project. The association table exists however, I am unsure how I would create the association.
View
Schema
I definitely must recommend you to not use has_and_belongs_to_many, because there is no way for you do to callbacks, validations and so on.
It is definitely nice to use a real join model and use has_many, through.
class User
has_many :project_users, dependent: :destroy
has_many :projects, through: :project_users
end
class Project
has_many :project_users, dependent: :destroy
has_many :users, through: :project_users
end
class ProjectUser
belongs_to :project, required: true
belongs_to :user, required: true
validates :project, uniqueness: { scope: :user }
end
This works very seamlessly, you can do:
User.update(project_ids: [1,5,6,7])
And it will join the user to these project unless any validations fail.
I started out a big project with these tables everywhere, after a few months we started running into duplication issues, bad state of records and it was a hot mess. Using a real join model is so worth it.
Since you have your project ID inside hidden you could just do this inside JOIN (post) action
def join
#project = Project.find(params[:project][:id])
current_user.projects << #project
end
so if you have instance of #project and instance of user - in my example it is current_user( for example if you use devise) then you would just assign them using operator <<
Here is the reference:
http://guides.rubyonrails.org/association_basics.html#has-many-association-reference
Hope it helps
I've got a table 'users' but one user might be a manager of another user, one user could also be a manager of many other users, so its a one-to-many relationship, and it only involes one table. heres my approach:
1.I've added 'manager_id'(which really stores another uners id) column to users table.
2.I've defined relationship in User model class:
belongs_to :manager, foreign_key: 'manager_id',class_name:'User'
has_many :minions, class_name:'User'
Now, say in rails console if i set one users 'manager_id' to another users id its all fine. but if i try to use methods like 'user.manager' or 'user.minions' it says those methods aren't defined. Were have i gone wrong?
I think you want to implement self inheritance that can be done as :
class User < ActiveRecord::Base
self.inheritance_column = :child_class
has_many :minions, :foreign_key => :parent_id, :as => :parent
belongs_to :parent, :polymorphic => true
end
This is just a sketch of what i understood from the above description, it can be enhanced.
I have a rails 4 application whereby I've set up a User model. I want the User to have a has_many association with a user profile model, but here's the catch:
My user profile model needs to be polymorphic - the
User model can have multiple (different) user profiles associated
with it (e.g. ProfileTypeA, ProfileTypeB, ProfileTypeC, etc.)
I want my User model to have one association, say, user_profiles, that
would return the all the user's user profiles associated with it.
I believe I'm on the right track (or am I?), but how would this be accomplished using the rails generator? The part that is most confusing to me, is how to do bullet #2 above.
P.S. I took a look at STI's, but it seems to me that my User model would have to have a hard-association with each user profile type model, which I don't like because it would change the User model with each new user profile type I add to the data model.
You sound on right track,try below
#The polymorphic models
class User
has_many :user_profiles, as: :profileable
end
class UserProfile
belongs_to :profileable, polymorphic: true
end
The Migrations below
#migrations
class CreateUserProfiles < ActiveRecord::Migration
def change
create_table :user_profiles do |t|
t.integer :profileable_id
t.string :profileable_type
t.timestamps null: false
end
add_index :user_profiles, :profileable_id
end
end
I'm completely stuck. I've read this http://guides.rubyonrails.org/association_basics.html#the-has-one-association and http://guides.rubyonrails.org/active_record_querying.html and am no closer to solving the problem.
I have a belongs_to, has_one model association. In my case it's a User has_one Booth and a Booth belongs_to a User. What I'm trying to do should be simple, create a link so that users who have created a booth can view the booth they created.
To accomplish this, I'm trying to create a 'user_booth' method. I've tried now about 100 different variations, but something like this should work from everything I've read:
def user_booth
#booth = current_user.booth.find_by(id: params[:id])
end
Well, of course that didn't work. I've also tried many variations similar to this:
def user_booth
Booth.find_by(id: params[:id])
end
When I check my database, everything saves exactly as I intended it to, I just can't seem to call on the correct booth that is associated with correct user. Can anyone lead me in a new direction? I'm entirely stuck.
Here is the show method in the Booths controller:
def show
#booth = Booth.find(params[:id])
end
I should also include my booth migration:
class CreateBooths < ActiveRecord::Migration
def change
create_table :booths do |t|
t.string :name
t.references :user, index: true
t.timestamps null: false
end
end
end
Thanks a million!