Questions tagged as 'active-record'

1
answer

Join data from two tables by foreign key - Rails ActiveRecord

Hello, I have a Model Comment which is a belongs_to of User , and a Model Profile that is also a belongs_to of User . I can easily access User data through Comment.first.user , for example, but I'd like to acce...
asked by 26.03.2018 / 04:44
0
answers

Foreign key PHP ActiveRecord

How can I delete a MySql record with PDO and PHP ActiveRecord respecting foreign keys. Example: I will delete a batch record, for example, and there is something registered in the batch I am going to delete so I wanted it not to delete and di...
asked by 04.01.2018 / 19:17
1
answer

How to find Entity and Daughters in a single sql

I have 3 classes that relate as follows: class AnamnesisModel < ApplicationRecord has_many :anamnesis_questions accepts_nested_attributes_for :anamnesis_questions end class AnamnesisQuestion < ApplicationRecord belongs_to :anamnes...
asked by 12.07.2017 / 20:20
1
answer

Help with Relationship between 3 Tables #Rail

I would like your help with this issue below I have 3 Tables: Product Purshase Supplier supplier_id product_id name class Purshase < ActiveRecord::Base belongs_to :product end class Product < ActiveR...
asked by 26.05.2017 / 20:27
1
answer

Active record, how to replace uniq with distinct correctly?

I'm trying to retrieve the months I have answers to a search this way: Answers.pluck('EXTRACT(MONTH FROM created_at)').uniq (0.7ms) SELECT EXTRACT(MONTH FROM created_at) FROM 'answers' => [3] This way I have the expected result, but I...
asked by 27.03.2017 / 16:34
1
answer

How to return the entire object from an associative table?

The code below returns only the ID's of their respective relationships, I need instead of returning user_id and role_id to return the object user and role with the registry data. Take a look at my code: class Disput...
asked by 02.02.2017 / 13:54
1
answer

Problem of perfomance when returning json with rabl after consultation with activerecord

I'm having trouble adjusting performance by returning json after a query to the database. I've tried a lot of things, and I'm not getting any faster. Can anyone give me strength with this? This is the query in the controller Methodnotmo...
asked by 29.06.2016 / 21:06
2
answers

How to pass an Array to the IN clause of a flat query?

I'm using ActiveRecord to run queries on several different databases. In one of the queries, I have to do the following: publications_to_read = [1,2,3,4] ActiveRecord::Base.connection.execute("ALTER TABLE publications SET readed = TRUE WHERE i...
asked by 13.04.2016 / 02:22
2
answers

Validation of has_and_belongs_to_many in Ruby on Rails

I have the following models: class Pedido < ActiveRecord::Base has_and_belongs_to_many :produtos validate :has_one_or_less_produtos private def has_one_or_less_produtos errors.add(:produtos, 'Não é permitido mais de 1 produto'...
asked by 10.12.2015 / 12:03
2
answers

Advanced Query in Ruby On Rails (ActiveRecord)

I need to do a query for a report something like this: Produto.include(:clientes).where('count(produto.id) < cliente.quantidade') In sql it would look something like this: SELECT C.NOME, C.QUANTIDADE, COUNT(P.ID) FROM PRODUTOS...
asked by 29.06.2015 / 16:53