I have two tables, registrations and discounts that hold information on a registration and a discount, respectively. Each registration can have one discount, and each discount can have many registrations.
I'm able to pull all of the registrations for a discount when I do something such as Discount.find(1).registrations, but when I attempt to call Registration.find(1).discount I get the error
Unknown column 'discounts.registration_id' in 'where clause': SELECT `discounts`.* FROM `discounts` WHERE `discounts`.`registration_id` = 1 ORDER BY `discounts`.`id` ASC LIMIT 1`
My models are currently set up as follows:
class Registration < ActiveRecord::Base
has_one :payment
has_one :discount
end
class Discount < ActiveRecord::Base
has_many :registrations
end
Additionally, my registration table has a foreign key discount_id.
I can make the association work if I set up a belongs_to relationship in the registrations model, but registrations don't belong to discounts - they may or may not have one.
How should I set up this relationship? Should I set up another table and use a has_many, through relationship?
Edit: I do not want to use a belongs_to relationship in my registration model, because a registration does not belong to a discount.
If a discount can have many registration then you want to use belongs_to instead of has_one
class Registration < ActiveRecord::Base
has_one :payment
belongs_to :discount
end
class Discount < ActiveRecord::Base
has_many :registrations
end
Check out this explanation of the difference between belongs_to and has_one
You should define your relationship like this:
class Registration < ActiveRecord::Base
has_one :payment
has_one :discountable
has_one :discount, through: :discountable
end
class Discount < ActiveRecord::Base
has_many :registration, through: :discountables
end
class Discountable < ActiveRecord::Base
belongs_to :registration
belongs_to :discount
end
Thanks
Related
I have a model company, which has association, has many candidates, and belongs to company.
And I have another model key_skill, which has association, has many key_skills, and belongs to candidate.
Another model is candidate, which belongs to company, and has many key skills association.
I am trying to get the candidate whose key skills are matched to the required skill and, it should search and get the candidate who belongs to the particular company.
How can I write a query in the model for this situation?
These are the associations
company.rb
class Company < ActiveRecord::Base
has_many :candidates
end
candidate.rb
class Candidate < ActiveRecord::Base
belongs_to :company
has_many :key_skills, dependent: :destroy
accepts_nested_attributes_for :key_skills, reject_if: :all_blank,
allow_destroy: true
end
key_skill.rb
class KeySkill < ActiveRecord::Base
belongs_to :candidate
end
I think your current association condition is like this:
class Company < ApplicationRecord
has_many :candidates
end
class Candidate < ApplicationRecord
belongs_to :company
has_many :key_skills
end
class KeySkill < ApplicationRecord
belongs_to :candidate
end
For example to fetch all candidates with key_skills with ids 1,2,3 run the following query
Candidate.joins(:company, :key_skills).where("key_skills.id in (?)", [1,2,3])
Try the below:
I am assuming that key_skills table have a field skill and you want to perform search on it.
candidate = Candidate.includes(:company, :key_skills).where("key_skills.skill like ?", "%#{params[:skill]}%")
company = candidate.company
I have 3 models: User, Order and Car and I have question because I don't know what relationships between these models will be the best. Only requirement is that only one car per user in order.
A user can have many orders, and therefore, many cars through those orders.
class User < ActiveRecord::Base
has_many :orders
has_many :cars, through: :orders
end
An order belongs to a user and a car.
class Order < ActiveRecord::Base
belongs_to :user
belongs_to :car
end
A car has one order.
class Car < ActiveRecord::Base
has_one :order
end
So you need one on one relationship between order and car and then back to order and customer one relationship. Something below should do the trick.
class Car < ActiveRecord::Base
has_one :order
has_one :customer, through: :order
end
class Order < ActiveRecord::Base
belongs_to :car
belongs_to :customer
end
class Customer < ActiveRecord::Base
has_one :order
has_one :car , through: :order
end
But i will suggest the has_many relationship by the looks of the model name, but you know better your problem than me.
I am tryng to develop a rent system where a user can login, create products, and his products can be rented by other users.
My problem is, I don't know how to create a rent that retrieves the product_id and customer_id. I made the relations but it isn't working.
I also create the CRUD for each one, even the rent. How can I store the information and pass to the Rent?
I have 4 models:
User
Product
Rent
Category
I created a new column in Rent called customer_id, and I've passed the class "User":
class Product < ActiveRecord::Base
belongs_to :user
belongs_to :category
belongs_to :rents
class User < ActiveRecord::Base
has_many :products
has_many :rents
has_many :customer_id, :through => :rent
class Rent < ActiveRecord::Base
belongs_to :user
belongs_to :product
belongs_to :customer, :class_name => "User"
end
I think I need to create a button that retrieves the information that I need. I searched through the documentation but I couldn't find it.
This line: has_many :customer_id, :through => :rent would never make sense this way in Rails. If you say has_many :customer_id, you are making two mistakes:
Whatever you write after has_many, it should be plural.
If what you write after has_many doesn't correspond directly to a model name as this the case with you, you have to explicit mention the class_name.
Same mistake you are repeating when you say:
class Product < ActiveRecord::Base
belongs_to :rents # it should be rent.
end
And now coming to what you are actually trying to implement:
class Rent < ActiveRecord::Base
belongs_to :user
has_one :product
has_one :customer
end
And in Product and Customer tables, you need to define rent_id as a foreign key. And you should also mention that each of them belongs_to Rent.
In my app I have 3 model:
OrganizationType
Organization
OrganizationTypeLink
as you guess: my organization could perform more than one action, and this type of action's I want to link with organization...
And in controller I write so:
def create
#admin_organization = Organization.new(admin_organization_params)
#admin_organization.organization_type_links.build(organization_type_id: params[:organization_type_id], organization_id: #admin_organization.id)
if #admin_organization.save
....
And in model OrganizationTypeLink I see new rows in db, but how to store in Organization organization_type_link_id ? How could I store it in db?
I am new to RoR, so please give advice )
upd:
class Organization < ActiveRecord::Base
belongs_to :organization_type
has_many :organization_type_links, :dependent => :destroy
end
class OrganizationTypeLink < ActiveRecord::Base
belongs_to :organization
belongs_to :organization_type
end
class OrganizationType < ActiveRecord::Base
has_many :organizations
has_many :organization_type_links
end
how to store in Organization organization_type_link_id ? how could i store it in db?
The way you have currently defined associations in these 3 models: OrganizationType, Organization and OrganizationTypeLink
Organization has_many organization_type_links means that OrganizationTypeLink will have a foreign key named organization_id in it and not the other way round.
If you want to have organization_type_link_id in Organization then you would need to setup association as:
class Organization < ActiveRecord::Base
belongs_to :organization_type
belong_to :organization_type_link
end
class OrganizationTypeLink < ActiveRecord::Base
has_many :organizations, :dependent => :destroy
belongs_to :organization_type
end
Say I have following association.
class Supplier < ActiveRecord::Base
has_one :account
has_one :account_history, through: :account
end
class Account < ActiveRecord::Base
belongs_to :supplier
has_one :account_history
end
class AccountHistory < ActiveRecord::Base
belongs_to :account
end
How can I access account table through supplier model
How can I access supplier table through account model
How can I access join table accounthistory without through other model
How can I add a field by migration to accounthistory and then get value from that field through supplier or account model
Surely this is too simplistic, but this what I'd propose:
1. #supplier.account
#supplier = Supplier.find(id)
#supplier.account #-> brings account data
2. #account.supplier
#account = Account.find(id)
#account.supplier #-> brings supplier data
3. #supplier.account_history
#supplier = Supplier.find(id)
#supplier.account_history #-> brings account_history
4. Migration
def change
add_column :account_histories, :your_column, :type, after: :column
end
You can deal with the join model / table directly (unlike HABTM's), meaning you will be able to create migrations as you wish. The above migration code shows how you can add a column to the table directly
Delegate
To access the data from that model, you're in luck
Because you're using a singular association (has_one / belongs_to), you should be able to delegate the call to another model:
#app/models/supplier.rb
Class Supplier < ActiveRecord::Base
has_one :account
has_one :account_history, through: :account
end
#app/models/account.rb
class Account < ActiveRecord::Base
belongs_to :supplier
has_one :account_history
delegate :extra, :vars, :here, to: :account_history
end
This will allow you to call: #supplier.account.extra etc
Hope this helps? Your question was rather scant on context, so I can fix my answer if you update!