Profile model association question
This is really a design decision that needs to be decided by how its going to be used.
I've recently created a program that has a 'user' and 'profile' model separated with the 'user' always having the a 'profile' constraint.
I did it this way since there is nothing in the 'user' table other then information needed to login a user or allow the admin to see/do certain things.
Whereas the 'profile' model contains data that can be viewed and searched on by other members.
But again, its a design decision you have to make based on how the data will be used.
Rails 6 & Model Association and/or Migrations
add_reference
is meant to be a shortcut as part of the rails convention, under the hood, it will call add_column
and other options that you specify [you can check the code for rails-6 here]
while foreign keys are not required, they are considered a best practice because they guarantee referential integrity. you can read more about it here https://edgeguides.rubyonrails.org/active_record_migrations.html#foreign-keys
or is it the same? Doesn't the second option create indexes as well whereas the first does not?
having said that, your 2 migrations are not entirely the same even though they work the same (they achieve the same goal) - the second option will add an index by default unless you specify the option not to - I definitely agree with you about using add_reference
as this is an easier and more foolproof shortcut
of course, you can also achieve that manually by using your first migration by adding an index and a foreign key as well
add_column :users, :course_id, :integer
add_index :users, :course_id # uniq or not
add_foreign_key :courses, :users
CakePHP Model Association question
No, technically you only need to specify the associations between models in the directions you are going to use. If you are never going to use an association in a certain direction, then you do not need to specify it.
Rails: Model.association vs Model.association.all
CollectionProxy vs AssociationRelation
Here is an explanation from a previous SO question that might help you out.
Trouble with model associations
Generally you are right.
We want users to follow a shop, and a shop can create rewards and grant many rewards to many followers.
1. Visual schema:
2. Model associations (complete version)
user.rb
has_many :follows
has_many :reward_follows, through: :follows
has_many :rewards, through: :reward_follows # NOT through shops
has_many :shops, through: :follows
follow.rb
belongs_to :user
belongs_to :shop
has_many :reward_follows
shop.rb
has_many :rewards
has_many :reward_follows, through: :rewards # NOT through follows
has_many :follows
has_many :users, through: :follows
reward.rb
has_many :reward_follows
belongs_to :shop
has_many :follows, through: :reward_follows
has_many :users, through: :follows
3. Do not use date field. Use datetime field.
Justification: https://www.ruby-forum.com/t/time-without-date/194146
This personally saved me hours of work long-term.
Laravel model association advice
Probably in your store model:
public function managerRelation()
{
return $this->hasOne('user', 'user_id', 'manager');
}
For those columns
users->user_id
stores->manager
On your User model
public function managerOf()
{
return $this->belongsToMany('store', 'stores', 'manager', 'id');
}
You should consider adding _id
to all your id columns, like, manager_id.
EDIT:
Another problem is that manager
is already a column in your table, so your relation cannot be manager()
, or you'll have to the column to manager_id
. I temporarily renamed it to managerRelation
so you should be able to:
echo $store->managerRelation->user_id;
Ruby on Rails model associations nubie question
Your areas
table needs a book_id
integer field to match against the books table's primary key.
Could not find the association :catorizations in model
Could not find the association :catorizations in model Question
You have a typo in Question
model
This
has_many :categories, through: :catorizations
should be
has_many :categories, through: :categorizations
Related Topics
Why Does Trying to Use Grape with Rails Fail with "Uninitialized Constant API"
Adding Fields to Devise Sign Up Using Rails 4
Ruby Selenium Web Driver: How to Count Child Element Nodes of a Specific Node
Rails Object#Blank? VS. String#Empty? Confusion
How to Http Post Stream Data from Memory in Ruby
Algorithm to Shuffle an Array Randomly Based on Different Weights
Regex "Punct" Character Class Matches Different Characters Depending on Ruby Version
What Is the Fastest Way to Sort a Hash
How to Display a Regexp Output in an Alphabetical List
404 Resource Not Found: Domain with Google Directory API
Why Is Autoload Failing to Load Files for Gems
Has_Many and No Method Error Issue
Ruby Getting Deeply Nested JSON API Data
Ruby Syntax Question: Rational(A, B) and Rational.New!(A, B)
Uml Sequence Diagram - How to Represent Method Arguments That Instantiate Objects