I am using Ruby on Rails 3 and I am trying to use an has_one :through association using namespaced classes. I read the official guide about association models but I don't know how to accomplish that.
I have User, Relationship and Groupclasses and I would like to associate Users and Groups through the Relationship class. As well, I would like to autosave related records and delete the relationship records on group or user deletion.
My file system is:
app/models/users/user.rb
app/models/users/relationship.rb
app/models/users/group.rb
In configs/routes.rb I have
namespace :users do
resources :users
resources :relationship
resources :groups
end
Class (Model) statements are:
class Users::User < ActiveRecord::Base
...
end
class Users::Relationship < ActiveRecord::Base
...
end
class Users::Group < ActiveRecord::Base
...
end
How I must write code associations in above model files? Have you some advice about?
UPDATE
My Classes (Models) have these attributes:
User
id
full_name
...
Relationship
id
user_id
group_id
Group
id
name
Your route namespacing has nothing to do with your model namespacing.
class Users::User < ActiveRecord::Base
has_many :relationships, :class_name=>"Users::Relationship", :dependent=>:destroy, :autosave=>true
has_many :groups, :class_name=>"Users::Group", :through=>:relationships
end
class Users::Relationship < ActiveRecord::Base
belongs_to :user, :class_name=>"Users::User"
belongs_to :group, :class_name=>"Users::Group"
end
class Users::Group < ActiveRecord::Base
has_many :relationsips, :class_name=>"Users::Relationship", :dependent=>:destroy, :autosave=>true
end
Related
What is 'as' in ruby on rails model? and how does it work?
e.g.
has_many :something, as: :reasonable
Is it polymorphic?
Yes, this is a polymorphic association which allows a model to belong to multiple models. There should be
class Something < ApplicationRecord
belongs_to :reasonable, polymorphic: true
end
And then any model can have many of these as reasonable without adding another column to Something.
class Thing < ApplicationRecord
has_many :somethings, as: :reasonable
end
class Stuff < ApplicationRecord
has_many :somethings, as: :reasonable
end
Something stores both the class and ID of what its associated with allowing it to be polymorphic.
I'm using STI models with has_and_belongs_to_many relations.
So I have User with many Templates of different types, like MainTemplate < Template; NotSoMainTemplate < Template; etc.
Is there a way to limit each user to have only one MainTemplate and only one NotSoMainTemplate, and so on for all types?
Let me reiterate the problem statement as I have understood it.
You want User to have at most one kind of each template. i.e.
1 MainTemplate, 1 NotSoMainTemplate, etc.
You don't need a direct relation with Template (parent table)
Each template may be used by more than one user
Based on the above assumption, I would suggest you to do the following:
Remove existing habtm association between User and Template
Add migrations to add main_template_id, not_so_main_template_id to User
Add the following associations:
class MainTemplate < Template
has_many :users
end
class NotSoMainTemplate < Templete
has_many :users
end
class class User < ActiveRecord::Base
belongs_to :main_template
belongs_to :not_so_main_template
end
Since you are already using STI, you can try has_one.
class Template < ActiveRecord::Base
has_many :users, through: template_choice
...
end
class MainTemplate < Template
...
end
class TemplateChoice < ActiveRecord::Base
belongs_to :template_choice
belongs_to :user
end
class User < ActiveRecord::Base
has_one :main_template, through: :template_choice
has_one :not_so_main_template, through: :template_choice
...
end
I have a model Article and model User. In users, there will be one creator of the article and many readers. How do I link these together?
I was thinking:
class Article < ActiveRecord::Base
has_and_belongs_to_many :users
has_one created_by, through: :user (????)
end
class User < ActiveRecord::Base
has_many :articles
end
You can do like this
class Article < ActiveRecord::Base
has_and_belongs_to_many :users
has_one created_by_user,:class_name => 'User'
end
If you want to specify a custom foreign_key(which is useful in these cases),you can specify a foreign_key option
class Article < ActiveRecord::Base
has_and_belongs_to_many :users
has_one created_by_user,:class_name => 'User',:foreign_key =>'your_custom_fkey'
end
I am using Ruby on Rails 3.0.7 and I would like to know how to retrieve the Active Record Association name between two classes\models.
That is, I have two models
class User < ActiveRecord::Base
has_many :accounts
end
class Account < ActiveRecord::Base
belongs_to :users
end
and I would like to retrieve (on runtime) them association name, in this case accounts and users strings.
Is it possible? If so, how can I do that?
UPDATE
If I have more association statements in User and Account classes (see the below example), how can I retrieve exactly the User Account association name?
class User < ActiveRecord::Base
has_many :accounts
has_many :articles
has_many :comments
end
class Account < ActiveRecord::Base
belongs_to :users
has_many :articles
belongs_to :authorization
end
?
User.reflect_on_all_associations.each do |assoc|
puts "#{assoc.macro} #{assoc.name}"
end
#=> "has_many accounts"
UPD
User.reflect_on_all_associations.select{|a| a.class_name == "Account"}.each do |assoc|
puts "#{assoc.macro} #{assoc.name}"
end
#=> "has_many accounts"
I have a many to many relationship in rails. All database tables are named accordingly and appropriately. All model files are plural and use underscore to seperate words. All naming comventions are followed by ruby and rails standards. I'm using has many through in my models like this:
has_many :users, :through => :users_posts #Post model
has_many :posts, :through => :users_posts #User model
belongs_to :users #UsersSource model
belongs_to :posts #UsersSource model
What else could this error be from?
ActiveRecord::HasManyThroughAssociationNotFoundError in UsersController#welcome
Could not find the association :users_posts in model Post
You need to define the join model as a separate association when using has_many :through:
class Post < ActiveRecord::Base
has_many :user_posts
has_many :users, :through => :user_posts
end
class User < ActiveRecord::Base
has_many :user_posts
has_many :posts, :through => :user_posts
end
class UserPost < ActiveRecord::Base
belongs_to :user # foreign_key is user_id
belongs_to :post # foreign_key is post_id
end
This works best when you need to keep data that pertains to the join model itself, or if you want to perform validations on the join separate from the other two models.
If you just want a simple join table, it's easier to use the old HABTM syntax:
class User < ActiveRecord::Base
has_and_belongs_to_many :posts
end
class Post < ActiveRecord::Base
has_and_belongs_to_many :users
end