activerecord

Active record has_many association for one model having two types of values

泪湿孤枕 提交于 2020-01-15 08:16:28
问题 i am facing a new situation. I know someone definitely had faced these type of situation: I have an invoices table and an invoice_line_items table. Till now every thing is going well with has_many and belongs to association. Now i want to add tax accounts in invoice items, for that i don't want to create a separate table, hence i make below changes in my invoice model : invoice.rb: class Invoice < ActiveRecord::Base has_many :invoice_line_items has_many :tax_line_items, :class_name =>

Active record in codeigniter

巧了我就是萌 提交于 2020-01-15 05:29:29
问题 Anybody can convert this query to active record codeigniter??? SELECT b.name, SUM(CASE WHEN c.size = 'S' THEN 1 ELSE 0 END) S, SUM(CASE WHEN c.size = 'M' THEN 1 ELSE 0 END) M, SUM(CASE WHEN c.size = 'L' THEN 1 ELSE 0 END) L, SUM(CASE WHEN c.size = 'XL' THEN 1 ELSE 0 END) XL FROM orderTB a INNER JOIN productTB b ON a.id_product = b.id_shirt INNER JOIN sizeTB c ON a.id_size = c.id_size GROUP BY b.name i've tried like this function get() { $this->db->select("b.name,SUM(CASE WHEN c.size ='S' THEN

Use ActiveRecord scope in ActiveAdmin filter

守給你的承諾、 提交于 2020-01-14 14:46:08
问题 In my rails project I have model : class Panel < ActiveRecord::Base has_many :surveys scope :by_survey_name, ->(survey_name) { joins(:surveys).where('surveys.survey_name LIKE (?)', "%#{survey_name}%") } end And the question is how can I use this scope in activeadmin fiter? 回答1: Addd to model: def self.ransackable_scopes(_auth_object = nil) [:by_survey_name] end and then in resource: filter :by_survey_name, as: :string 来源: https://stackoverflow.com/questions/31694398/use-activerecord-scope-in

ActiveRecord serialize sending nil to custom serializer for present attribute

柔情痞子 提交于 2020-01-14 13:43:23
问题 I have the following custom serializer: class ReportCardDataSerializer def self.dump(hash) hash.to_json end def self.load(json) # json.class == NilClass, why???? hash = (json || {}).with_indifferent_access end end And the following class with a serialized data attribute with database column set to NOT NULL. class ReportCardGroup < ActiveRecord::Base serialize :data, ReportCardDataSerializer # data is PostgreSQL jsonb column end The ReportCardDataSerializer dump method works as expected. But

Evaluating :dependent => :destroy

僤鯓⒐⒋嵵緔 提交于 2020-01-14 13:07:15
问题 In Rails 2.2.2 (ruby 1.8.7-p72), I'd like to evaluate the impact of destroying an object before actually doing it. I.e. I would like to be able to generate a list of all objects that will be affected by :dependent => :destroy (via an object's associations). The real problem I'm trying to solve is to give a user a list of everything that will be deleted and having them confirm the action. Can anyone recommend a good way to go about this? I've just started looking into ActiveRecord:

Evaluating :dependent => :destroy

社会主义新天地 提交于 2020-01-14 13:06:49
问题 In Rails 2.2.2 (ruby 1.8.7-p72), I'd like to evaluate the impact of destroying an object before actually doing it. I.e. I would like to be able to generate a list of all objects that will be affected by :dependent => :destroy (via an object's associations). The real problem I'm trying to solve is to give a user a list of everything that will be deleted and having them confirm the action. Can anyone recommend a good way to go about this? I've just started looking into ActiveRecord:

Combine two ActiveRecord::Relation with OR, not AND, returning a Relation and not an Array to be able to paginate later

家住魔仙堡 提交于 2020-01-14 12:46:51
问题 a and b are ActiveRecord::Relation objects which return the same type of objects(Micropost objects in this case) a.class => ActiveRecord::Relation b.class => ActiveRecord::Relation a.first => Micropost(...) b.first => Micropost(...) #They both return the same type of objects c=a+b c.class => Array #This is not what i'm looking for c=a|b c.class => Array #Not what i'm looking for either c=(a or b) c.class => ActiveRecord::Relation #But it is just a, so it's wrong c==a => true a.merge(b) => []

Is it possible to define virtual attributes in activerecord corresponding to SQL expressions?

左心房为你撑大大i 提交于 2020-01-14 10:33:10
问题 I'm after something like virtual attribute, but that would work on the database level: say I have a field age and I would like to add a "virtual field" age_quintile which equals age/5 , but in such a way that it is possible to say: Person.select(:age_quintile,"agv(height)"). group(:age_quintile). order(:age_quintile) corresponding to: SELECT (age/5) as age_quintile, avg(height) FROM persons GROUP BY (age/5) ORDER BY (age/5); or Person.maximum(:age_quintile) corresponding to SELECT max(age/5)

Rails: Prevent will_paginate from calling #count of ActiveRelation

不羁岁月 提交于 2020-01-14 07:59:12
问题 When I pass will_paginate an ActiveRelation, it always calls its #count method and hits the database to find out the total number of items. But this operation takes time and I have the total number already cached and ready. Can I pass this pre-calculated count to will_paginate and stop it from hitting the database? I tried the :count option, but it is passed to ActiveRecord as an option: active_relation.paginate(page: 2, per_page: 100, count: total_count) Thanks! :) 回答1: Passing the cached

Rails ActiveRecord Connecting to Wrong Postgres Database

被刻印的时光 ゝ 提交于 2020-01-14 06:22:36
问题 My database.yml is as follows: development: adapter: postgresql database: phunt_development username: <%= ENV['PG_USER'] %> password: <%= ENV['PG_PASS'] %> host: localhost pool: 5 timeout: 5000 And I can confirm that the database phunt_development does exist. However when I run ActiveRecord::Base.connection.current_database I get development not phunt_development . I can't figure out why Rails is insisting on connecting to the general development database, which is polluted with data I have