activerecord

How to scope model through two level association in Rails?

一曲冷凌霜 提交于 2019-12-11 23:20:23
问题 I have models User , Tagging , Tag User.rb has_one :tagging Tagging.rb belongs_to :user belongs_to :tag Tag.rb has_many :taggings I want to scope the User based on the Tag's name. How to do it? Currently, I know how to do the scope under one level association, for example: scope :with_tag_id, -> (tag_id) {joins(:tagging).where(taggings: {tag_id: tag_id})} But how to do more levels? 回答1: Try this class User < ActiveRecord::Base scope :by_tag_name, ->(tag_name) { joins(tagging: :tag).where(

Serialize and deserialize ActiveRecord model with children

假装没事ソ 提交于 2019-12-11 23:03:55
问题 Form has many entries. To serialize form and it's entries I use: json = @form.to_json( { :only => Form.accessible_attributes.to_a, :include => {:entries => {:only => Entry.accessible_attributes.to_a}}}) Form and entries attributes can be modified or deleted while they are in JSON. To deserialize I use (but not working) : @form = @form.from_json(json) @form.save How to save entries at once with form? 回答1: Had to change entries to entries_attributes: json = @form.to_json( { :only => Form

How to select unique records based on foreign key column in Rails?

给你一囗甜甜゛ 提交于 2019-12-11 22:32:11
问题 I have the following model structure in my Rails 4.1 application: delivery_service.rb class DeliveryService < ActiveRecord::Base attr_accessible :name, :description, :courier_name, :active, :country_ids has_many :prices, class_name: 'DeliveryServicePrice', dependent: :delete_all end delivery_service_price.rb class DeliveryServicePrice < ActiveRecord::Base attr_accessible :code, :price, :description, :min_weight, :max_weight, :min_length, :max_length, :min_thickness, :max_thickness, :active,

ActiveRecord aggregate function: is there a database-agnostic 'EXTRACT(WEEK from date)'?

纵然是瞬间 提交于 2019-12-11 22:24:25
问题 i have a method that works just fine (with rails 3 and PostgreSQL) ; i just wonder if it's possible to make it database-agnostic : FarmGatePrice.average :price, :group => 'EXTRACT(WEEK FROM date)' As i understand it, methods to extract a week ISO number from a date are always database-specific. I could use something like a virtual attribute :week as seen here, but it seems the :group options only accepts raw SQL. I've also seen this solution but it doesn't fit my needs. Another way would be

ActiveRecord Includes

断了今生、忘了曾经 提交于 2019-12-11 21:27:43
问题 I have made a workout application with with the following models: user routine lifts exercises infos A routine belongs to a user A routine has many lifts (which is a joins between exercise and routine) A lift belongs to an exercise Infos(sets) belong to a lift I am digging into ActiveRecord Queries and specifically includes to work on the n+1 problems I am having. When I show the entire routine which shows: The routines name The users name The exercise names for each lift The sets for each

Associate user_id to the comment model?

ぃ、小莉子 提交于 2019-12-11 21:09:22
问题 I use devise for authentication, I want to associate user_id to the comment model. It works when I send user_id as a parameter, But I want to set the current logined user's user_id automatically to the each comment. How can I do that? #model/post.rb class Post < ActiveRecord::Base has_many :comments belongs_to :category end #model/comment.rb class Comment < ActiveRecord::Base belongs_to :post end #model/category.rb has_many :posts #controllers/comments_controller class CommentsController <

ActiveRecord query with nested models and derived column values?

守給你的承諾、 提交于 2019-12-11 20:41:30
问题 I have the following relationships: class Order < ActiveRecord::Base has_many :item_selections, :dependent => :destroy has_many :inventory_items, :through => :item_selections end class InventoryItem < ActiveRecord::Base has_many :item_selections, :dependent => :destroy has_many :orders, :through => :item_selections end class ItemSelection < ActiveRecord::Base belongs_to :order belongs_to :inventory_item end I am trying to create the ActiveRecord equivalent of this SQL query below and then

Overriding rails #destroy argument error

邮差的信 提交于 2019-12-11 20:29:20
问题 My Goal: Override the #destroy method in rails so that gems like Devise will perform a soft-delete. My Attempt: def destroy(mode = :soft) if mode == :hard super else ... soft-delete actions ... end end When this is called on the object it get... ArgumentError: wrong number of arguments (1 for 0) I would like for this to default to soft-deleting, but have the option to fully destroy the object. My approach may not be the best way to do this, but any advice would help. 回答1: It should be super()

no method error rails on AJAX POST request

社会主义新天地 提交于 2019-12-11 20:23:42
问题 I'm trying to do an AJAX POST request from fullcalendar to rails, but I get no method error and I don't know why as the parameters seem to be fine. If I leave out event: from the ajax req so sending over only the params like data: {title: title, sender_id: senderId..} then I get the same error. What did I miss? Started POST "/users/1/events" for ::1 at 2015-12-17 14:33:12 -0800 14:33:12 puma.1 | Processing by EventsController#create as */* 14:33:12 puma.1 | Parameters: {"event"=>{"title"=>

can I add an includes extension to a belongs_to association?

微笑、不失礼 提交于 2019-12-11 20:17:50
问题 I'm having trouble getting a belongs_to association to eager load it's children. I have: class User < ActiveRecord::Base has_many :campaigns, -> { includes :campaign_shirts, :arts, :selected_campaign_shirt } belongs_to :selected_campaign, {class_name: "Campaign", inverse_of: :user}, -> { includes :campaign_shirts, :arts, :selected_campaign_shirt } end which results in: // GOOD u.campaigns.first.campaign_shirts.first.to_s => "#<CampaignShirt:0x007fc023a9abb0>" u.campaigns.first.campaign_shirts