I am working on a rails app, with different tables and associations, and trying to find out the best solution for that, don't know yet.
One table/class is called categories
For example
Categories
- Stocks
- Reits
-(...)
One table for each category
Stocks
belongs_to :category
Reits
belongs_to :category
Than i want to create a table called "Dividends", with a condition, don't know if is possible, for example:
Dividends
belongs_to :category
if category_id == 1
belongs_to :stock
end
if category_id == 2
belongs_to :reit
end
With the command:
rails g scaffold dividends category:references stock:references reit:references
But i don't know if it would be confusing, an if maybe would be better create a table for each class, for example:
StocksDividends
belongs_to :category
belongs_to :stock
ReitsDividends
belongs_to :category
belongs_to :reit
Would like some help on thinking about that solution.
just a quick hint - take a look at polymorphic associations:
https://guides.rubyonrails.org/association_basics.html#polymorphic-associations
The mechanics of it put a foreign key in the dividends table along with a type column. The type column tells you what type of record is the parent - the stock or the reit.
Related
I am very new to Ruby on Rails but I am decent with SQL. I am currently looking at trying to make an association for two tables... well actually more but this one example should answer my question for all. These are fictional but represent my issue so don't mind if they wouldn't follow best practices...
Tables: USER, LOCA --- They had once limited table name characters back in the day.
-LOCA
LOCATIONID - PK
-USER
USERID - PK; LIVES_LOCATION - FK (LOCATIONID); WORKS_LOCATION - FK (LOCATIONID); MANAGERID....... etc
How do in Ruby on Rails, make the class with belongs_to and has_many with the correct foreign_key names to the correct columns?
class User
belongs_to :lives, class_name: 'Loca', foreign_key: 'lives_location'
belongs_to :works, class_name: 'Loca', foreign_key: 'works_location'
...
end
class Loca
has_many :users
...
end
I am working on building a portfolio section in my website right now. I have it set where a portfolio has one category that is associated with it.
class Portfolio < ApplicationRecord
has_one :category
end
I am trying to access the category attribute name in the show view for the portfolio , but I am getting this error.
SQLite3::SQLException: no such column: categories.portfolio_id: SELECT "categories".* FROM "categories" WHERE "categories"."portfolio_id" = ? LIMIT ?
This is what is in the view:
<li><i class="icon_link"></i><span>Category: </span><%= #portfolio.category.name %></li>
I remembering using this syntax before and not having any problems with it. Any help would be great. I tried to find this question on here =, but could not make of them work.
Rails conventional naming for has_one/belongs_to associations is - if you have category_id column in Portfolio then "a portfolio belongs_to a category" and "a category has one portfolio". You need to rewrite your models as:
class Portfolio < ApplicationRecord
belongs_to :category
class Category < ApplicationRecord
has_one :portfolio
After that #portfolio.category.name should work fine.
Update: Possibly "a category has many portfolios", when the models will be:
class Portfolio < ApplicationRecord
belongs_to :category
class Category < ApplicationRecord
has_many :portfolio
Whenever you have an association in rails, there has to be at least two entites (or models) involved in that association. In your particular case, you've associated a Portfolio with a Category via the has_one association. However, you need to specify the association on the Category end as well. So in your Category.rb model class, you need to write:
class Category < ActiveRecord::Base
belongs_to :portfolio
# other stuff
end
There's one more step to ensuring that the association is working properly. You need to make sure that the categories table in your database has a field called portfolio_id. If it does, then you should be good to go! If it doesn't then do the following:
Type rails generate migration addPortfolioIdToCategory in your terminal
Open up the migration file and ensure it looks like this:
def change
add_column :categories, :portfolio_id, :integer
end
Now run rake db:migrate from your terminal
If you reload your server, your problem should be solved!
NOTE
The model that specifies the belongs_to association must have the primary key attribute in the corresponding database table. In your case, if the Category model has the belongs_to association, then the categories database table must have the field titled portfolio_id.
It's complaining portfolio_id doesn't exist on your categories table. Verify your migration file for categories. Given the foreign key is set on categories it should work. Rails has one association
I've looked everywhere, and can't find anything (maybe cause it's not possible)
I have a Meeting model and a Language model (which has a string column called language). Each Meeting has 2 Languages.
Is there a way to make an association, such as:
rails g migration AddLanguageToMeetings language:references
And then store an array of 2 language_ids in the reference?
For example, Meeting.language_id = [1,2]
And then be able to call the Language, like:
meeting.language_id[0].language
How can I se up this association? Do I need to have 2 different columns with each associated id?
Thanks!!
What you want is a N-to-N relation. Create another model called MeetingLanguage with two columns:
create_table :meeting_languages do |t|
t.references :meetings
t.references :languages
end
and associations:
class MeetingLanugage < ActiveRecord::Base
belongs_to :language
belongs_to :meeting
end
And then in Meeting module:
has_many :meeting_languages
has_many :languages, through: :meeting_languages, source: :language
Now you can have as many languages as you want for a single meeting.
What you're describing is a has_many, where the foreign key exists in the languages table, or in a joining table between the languages and meetings tables.
If you want each Meeting to point two exactly two Languages, then you can use two foreign keys in the meetings table, give each language a real name, and then have two belongs_to associations in your Meeting.
How to do this:
has_many :space_mappings, -> { where(group_id: group_id) }, through: :category
That is - SpaceMapping and this model both has a group_id and they have to match. I could just make a method but I would like for this to be possible.
Here I get:
undefined local variable or method `group_id' for #<ActiveRecord::Relation::ActiveRecord_Relation_SpaceMapping:0x007fe5ac118bd8>
I have done this instead:
def space_mappings
category.space_mappings.where(space_id: Space.where(group_id: group_id))
end
Thanks in advance.
You must assign a value for category_id.
See http://guides.rubyonrails.org/association_basics.html (4.3.3.1 section).
It can help your for understanding details and clear concept.
If "this model" has a belongs_to :category relationship or a has_one :category relationship, then you shouldn't need that where clause at all. The whole point of "has many through" is to restrict the associated models to those that are associated with the model they're associated through
That is, you should just be able to do
belongs_to :category
has_many :space_mappings, through: :category
assuming that space mappings also belong to a category.
I am having trouble wrapping my head around what I'd consider a more complex association and was hoping someone could help point me in the right direction.
I have four models:
user
profile
feed_group
feed
All 4 of these obviously have their own fields and data stored within. My goal is to have a summary database table called "user_detail" (I am open to calling it something different if Rails has its conventions), which has the following 4 fields:
user_id
profile_id
feed_group_id
feed_id
What would my model associations look like?
Thanks.
You can create the model through a migration like this:
rails g model UserDetail user:references profile:references feed_group:references feed:references
Within the created file models/user_detail.rb you will find the relations:
class UserDetail < ActiveRecord::Base
belongs_to :user
belongs_to :profile
belongs_to :feed_group
belongs_to :feed
end
Also, add references to UserDetail in all the referenced models, eg. in models/user.rb add has_many :user_details, etc.
As I read your question, that's all you need.