367 questions
0
votes
0
answers
93
views
Rails Admin field filter in a has_one through relationship
I have a Remediation model which inherits from AssessmentHighRiskFactor model and added some custom show fields from other tables using relationships. The fields are visible and working fine, but I ...
0
votes
2
answers
67
views
Passing a variable in an after_update action
I have an UserProfile model:
class UserProfile < ApplicationRecord
after_update :check_changes
def check_changes
AuditRecord.create(account_id: self.id, fields: self.saved_changes , ...
0
votes
0
answers
127
views
Rails Associations - has_one, belongs_to_many
It's been a while since I've worked with Rails (Rails 6) on a new application. I'm failing to recall how to properly set up associations. I have two problems that are rather similar and may help to ...
-1
votes
1
answer
804
views
How can i write a method that can generate scope dynamically which can be used for multiple models in ruby on rails
Currently, the existing scopes are like this.
module TransactionScopes
extend ActiveSupport::Concern
included do
scope :status, ->(status) { where status: status }
scope ...
0
votes
0
answers
258
views
How to avoid saving nil/blank in accepts_nested_attributes_for when displaying set number of fields for has_many association?
My question is an edgecase of how to avoid saving empty records on a nested rails form. I have a simple has_many, where a user can have a maximum of 5 job titles.
# user.rb
has_many :job_titles
...
0
votes
2
answers
347
views
How to find out all the association of a model?
Say I have following models
class User < ApplicationRecord
devise :database_authenticable,:registerable
:recoverable, :trackable, :validatable, :rememberable
belongs_to :loginable, ...
0
votes
0
answers
136
views
What are the consequences of too many unnecessary associations in rails database?
Sometimes it feels natural or is at least tempting to make model associations based on columns that can be associated. But that doesn't mean they should be associated.
What price does a database/app ...
0
votes
1
answer
410
views
Add additional attribute on a has_many through in Rails
I have set up the following associations:
class BookLaunch < ApplicationRecord
has_many :book_launch_locations, dependent: :destroy
has_many :stores, through: :book_launch_locations
....
...
1
vote
2
answers
520
views
has_one model association not enforcing 'one' in associated model?
I have the following
class User < ApplicationRecord
has_one :physician
end
and
class Physician < ApplicationRecord
belongs_to :user
end
Since a user can only have one physician, I was ...
1
vote
2
answers
1k
views
How to add dynamic value for a column in ruby on rails model with Active Record Callbacks
I have a model Document in my rails application. It has columns name, and key.
In my controller for create action, I get document name from the frontend and dynamically set one value for key with ...
2
votes
2
answers
501
views
Getting part of the raw SQL in Rails API response for not found records
I created a Rails 5 app with config.api_only set to true, and implemented a controller action that deletes a record in a scoped model Message the problem is that when I try to delete a record that ...
0
votes
2
answers
82
views
Model.find() calling a different model
I have 3 models user, micropost, like
I followed Ruby on Rails Tutorial by Michael Hartl
https://rails-4-0.railstutorial.org/book
Now I am adding new features and was trying to add a like button and ...
1
vote
2
answers
82
views
Puts statement in function is not executed, function test fails, but receive(:function) spec works
In my model definition, I have
# models/my_model.rb
# == Schema Information
#
# Table name: my_models
#
# id :bigint not null, primary key
# another_model_id :bigint
# ...
1
vote
1
answer
145
views
Assign user model to one of two models?
I have a user model that I want to assign to be teacher or student( teacher and student are two separated model ) because if the user signup he would have different registration fields depending on if ...
0
votes
1
answer
132
views
Define a nested has_many and belongs_to
Let's suppose I have 3 models, A, B and C (where A has many B, and B has many C):
class A < ActiveRecord::Base
has_many :bs
end
class B < ActiveRecord::Base
belongs_to :a
has_many :cs
...
0
votes
2
answers
2k
views
Rails merge (and manual assignment) assigning nil
I am trying to create a model in a controller using strong params in Rails 5.1 (some things changed from previous for strong_params). However, when I inspect the params, the merged ones are NOT ...
1
vote
2
answers
1k
views
All table fields are not showing? (Rails/MySQL)
I've created new fields, "first_name", "last_name", "email", and "password" in the model generated migrate file as shown:
class CreateUsers < ActiveRecord::Migration[5.2]
def up
...
4
votes
2
answers
2k
views
Ruby on rails return a string if a model attribute is nil
I have a user model that has one profile. The profile has attribute such as name, gender, age, dob, designation. No fields in profile are mandatory.
The user is created by an admin from only an email ...
0
votes
2
answers
64
views
Form which creates a quantity of another model with form field
I have two models, Hotel and Room. I want to create a form that will allow me to add a new hotel and rooms, which indicates the name of the hotel and the quantity of rooms.
I know I should use "nested ...
-1
votes
1
answer
181
views
Student Batch Grade Relationship Rails
A student gets admitted to a school and in its admission form, I want the user to assign student his/her batch and grade as well. Batch has many grades and grade belongs to batch.
In this scenario, I'...
1
vote
1
answer
44
views
Scoping f.collection_select
I've got a model that I'm using a collection_select on and I'd like to scope it to show only accounts that are open. To accomplish this, I've added a boolean field to my account model, defaulting to ...
2
votes
3
answers
1k
views
Get all records that exist in both the tables
Below are two tables with some sample data :-
Uploads:
id: 1 , file_ref:abc
id: 2, file_ref: abc1
id: 4, file_ref: abc3
id: 5, file_ref: abc4
id: 6, file_ref: abc5
id: 7, file_ref: abc6
...
0
votes
1
answer
502
views
User and Project relationship
So I have a User model created with devise and a Project model. A Project should have one Project owner and project members. So now I have no idea how to create the relations in my models.
What I ...
0
votes
1
answer
114
views
Overriding existing model in Rails 4
I am trying to override an existing active record model but the new active record model doesn't have methods from the old active record model. Here is the code that I am trying to use
class ModelA &...
1
vote
4
answers
60
views
How should these Rails Models be associated?
I have over thought this to the point that I'm only getting confused now.
I have a Rails app with the following as models: User, Site, Tutorial, TutorialStep.
A user can create many tutorials and ...