activerecord

Rails admin has_many :through relationship causing infinite rendering of templates when editing resource

我的梦境 提交于 2019-12-12 04:53:51
问题 I have a rails app with a has_many :through model like so: class Blog < ActiveRecord::Base has_many :blog_categorizations, dependent: :destroy has_many :categories, :through => :blog_categorizations accepts_nested_attributes_for :categories, allow_destroy: true end class Category < ActiveRecord::Base has_many :blog_categorizations, dependent: :destroy has_many :blog, :through => :blog_categorizations accepts_nested_attributes_for :blog, allow_destroy: true end class BlogCategorization <

ActiveRecord ignores table_name_prefix

陌路散爱 提交于 2019-12-12 04:48:58
问题 I've got situation where I need to define table_name and table_name_prefix within a model and for some reason table_name overrides table_name_prefix. class ScheduleItem < ActiveRecord::Base self.table_name = "schedule_item" self.table_name_prefix = 'ACQ_IBM_T.' end The prefix is completely ignored in the queries. But, when I comment out the table_name part then the prefix is added. Anyone has seen strange behavior like this before? 回答1: In ActiveRecord::ModelSchema::ClassMethods, the table

Unfortunately this works: ROR Comparison in Activerecord - update

会有一股神秘感。 提交于 2019-12-12 04:44:23
问题 Unfortunately this mess works: Do you have suggestings for cleaning up this code: I'm trying to use a activerecord to compare two table columns, "needed" and "amount" then update a boolean column, depending on the returned data. Totally breaking do-not-repeat coding. def update @inventory = Inventory.find(params[:id]) respond_to do |format| if @inventory.update_attributes(params[:inventory]) unless @inventory.needed.nil? if @inventory.needed < @inventory.amount then @inventory.instock = true

rails_admin plural field names

寵の児 提交于 2019-12-12 04:39:49
问题 I'm using rails_admin in an app and I'm getting an unexpected error. In a certain model, I have a field called *_status_id. When trying to edit that model in rails_admin, I get this error: undefined method `*_statu_id' Obviously, rails_admin thinks my resource is plural, when it is not. Is there a way of letting Rails/rails_admin know how to use this resource properly? 回答1: "status" has an irregular plural; one which Rails doesn't know about automatically. In config/initializers/infelctions

Multiple CTEs with Arel

拟墨画扇 提交于 2019-12-12 04:37:20
问题 I have an sql query of the following format: with from as (#{select * query}), to as (#{another select *}), rates as (#{yet another select *}) select rates.* from rates, from, to where rates.from_id = from.id and rates.to_id = to.id How can I convert this to Arel? I looked into Arel CTE, but there are no examples of using multiple aliases like in the query above. 回答1: This should do the trick: from_table = Arel::Table.new(:from) to_table = Arel::Table.new(:to) rates_table = Arel::Table.new(

Yii2 model load does not work as expected

六眼飞鱼酱① 提交于 2019-12-12 04:35:54
问题 My question may look like the following question: yii2 - model load function does not set some model attributes However, the case here is different due to dealing with Many to Many relation through junction table. For instance, I have three tables, Jobs , Eqtypes and the junction table Eqtype_jobs . I want to relate some Eqtypes to a current Job using simple activeform using multiple select dropDownList . The following is the code that I have in, the controller and the view: //the controller

ActiveRecord: error when trying to order results from a scope

断了今生、忘了曾经 提交于 2019-12-12 04:35:20
问题 I have the following model: class Master < ActiveRecord::Base belongs_to :language has_and_belongs_to_many :users scope :with_users_count, -> do joins('LEFT OUTER JOIN masters_users on masters_users.master_id = masters.id') .select('masters.*, COUNT(masters_users.user_id) as users_count') .group('masters.id') end end I'm trying to do: Master.with_users_count.includes(:language).order('languages.name') And the following error happens: ActiveRecord::StatementInvalid: PG::GroupingError: ERROR:

Rails Active Record Query joins only, any

淺唱寂寞╮ 提交于 2019-12-12 04:21:42
问题 Student has_many :enrollments With this query I see those students that have true enrollments, but also those that have both , true and false enrollments: @students = Student.joins(:enrollments).where(enrollments: { is_active: false }) Is there some "only" attribute that I can add to see students that have only active enrollments? 回答1: One way straightforward would be to find the students that inactive enrolments and then explicitly exclude them. Something like: have_inactives = Enrollment

Ruby on Rails query yielding unexpected results

半腔热情 提交于 2019-12-12 04:18:43
问题 This is a followup to an earlier thread: Ruby on Rails query not working properly. As noted, I have several listings. In particular, a listing has_many :spaces, through: :designations and has_many :amenities, through: :offerings . I define filters to restrict the listings that get shown. The two main ones are: # filter by amenities if params[:search][:amenity_ids].present? && params[:search][:amenity_ids].reject(&:blank?).size > 0 @listings = @listings.joins(:amenities).where(amenities: { id:

How to query a remote SQLAnywhere16 db using Rails?

夙愿已清 提交于 2019-12-12 04:16:41
问题 I am trying to set up a Rails app to query a remotely hosted SQLAnywhere 16 db and to display some query information as HTML. I'm using current versions of Ruby and Rails on Mac El Capitan. The remote server is running Windows. The server is located in my building and I am on the same LAN, so if there's a simpler solution that involves accessing the box itself physically I'm open to that. On my Windows machine I am able to connect to the server and do queries on the db using a SQL client. I