activerecord

Rails - Multiple Index Key Association

て烟熏妆下的殇ゞ 提交于 2019-12-30 02:28:10
问题 There seem to be a number of ways to handle a multiple foreign key association. Each way I have approached this has their draw backs, and as I am new to Rails I am convinced others have come across a similar scenario and I am probably working on something solved long ago. My question is: What would be an efficient way of handling a multiple index key association, while still retaining all other Rails sql modifiers (such as :include etc)? My scenario is: I have a table association as follows

Rails: Using will_paginate with a complex association find

泄露秘密 提交于 2019-12-30 02:27:27
问题 I'm encountering a problem with will_paginate while doing a complex find. :photo has_many :tags, :through => :tagships :item has_many :photos :photo belongs_to :item @photos = @item.photos.paginate :page => params[:page], :per_page => 200, :conditions => [ 'tags.id IN (?)', tag_ids], :order => 'created_at DESC', :joins => :tags, :group => "photos.id HAVING COUNT(DISTINCT tags.id) = #{tag_count}" I want to fetch all the photos who have all the tags in the tag_ids array. MySQL's IN usually does

Turn off verbose sql/ActiveRecord for Rails 3.1.1

和自甴很熟 提交于 2019-12-30 01:53:12
问题 Whereas the verbose feature of SQL/ActiveRecord calls is useful most of the time, I would like to turn it off in cases where I have some looping going on. Is there a way to turn it off? irb(main):055:0> City.first ←[1m←[35mCity Load (1.0ms)←[0m SELECT `cities`.* FROM `cities` LIMIT 1 => #<City id: 1, name: "bla bla", state_id: 1, zip: nil, country_id: nil, created_at: "2011-03-27 14:11:28", updated_at: "2011-08-16 11:14:36", guid: "5PK fvvz2Gsi"> 回答1: In console: Disable: old_logger =

Rails3 - Caching in development mode with Rails.cache.fetch

别等时光非礼了梦想. 提交于 2019-12-30 01:52:26
问题 In development, the following (simplified) statement always logs a cache miss, in production it works as expected: @categories = Rails.cache.fetch("categories", :expires_in => 5.minutes) do Rails.logger.info "+++ Cache missed +++" Category.all end If I change config.cache_classes from false to true in config/development.rb, it works as well in development mode, however, this makes development rather painful. Is there any configuration setting that is like config.cache_classes = false except

counter_cache has_many_through sql optimisation, reduce number of sql queries

萝らか妹 提交于 2019-12-30 01:36:11
问题 How I can optimise my SQL queries, to ignore situations like this: Meeting.find(5).users.size => SELECT COUNT(*) FROM ... WHERE ... User.find(123).meetings.size => SELECT COUNT(*) FROm ... WHERE ... I have no idea how to use counter_cache here. Here is my model relation: class Meeting < ActiveRecord::Base has_many :meeting_users has_many :users, :through => meeting_users end class User < ActiveRecord::Base has_many :meeting_users has_many :meetings, :through => meeting_users end class Meeting

Associating Two Models in Rails (user and profile)

こ雲淡風輕ζ 提交于 2019-12-30 01:31:07
问题 I'm new to Rails. I'm building an app that has a user model and a profile model. I want to associate these models such that: - After the user creates an account, he is automatically sent to the "create profile" page, and the profile he creates is connected to only that particular user. - Only the user who owns the profile can edit it. I generated the user model using nifty_generators. When the user hits submit for the account creation, I redirect him to the "new profile" view to create a

Rails 2.3 session

微笑、不失礼 提交于 2019-12-30 01:26:06
问题 I am developing a rails 2.3.2 app. I need to keep session_id for an order record, retrieve it and finally delete the session_id when the order is completed. It worked when I used cookies as session store but it doesn't for active_record store. (I restarted my browser, so no cache issue.) I know rails 2.3 implements lazy session load. I read some info about it but am still confused. Can somebody clarify how I use session_id for such a case? What I am doing is... A user make an order going

Remove object from has_many but don't delete the original record in Rails?

笑着哭i 提交于 2019-12-30 00:51:07
问题 I have this: Post.paragraphs << new_paragraph And I need to remove paragraph by id = 3, so the following deletes the record completely: Post.paragraphs.find(paragraph_id).destroy # or Post.paragraphs.find(paragraph_id).delete I just need to remove a paragraph from has_many association. I tried to use delete and destroy . Both methods completely delete records from the associated tables. How can I just remove them from the "container"? 回答1: You should not use the delete method on the Paragraph

How to build a JSON response made up of multiple models in Rails

百般思念 提交于 2019-12-30 00:37:47
问题 First, the desired result I have User and Item models. I'd like to build a JSON response that looks like this: { "user": {"username":"Bob!","foo":"whatever","bar":"hello!"}, "items": [ {"id":1, "name":"one", "zim":"planet", "gir":"earth"}, {"id":2, "name":"two", "zim":"planet", "gir":"mars"} ] } However, my User and Item model have more attributes than just those. I found a way to get this to work, but beware , it's not pretty... Please help... Update The next section contains the original

How to determine if a record is just created or updated in after_save

纵然是瞬间 提交于 2019-12-29 11:31:33
问题 The #new_record? function determines if a record has been saved. But it is always false in the after_save hook. Is there a way to determine whether the record is a newly created record or an old one from update? I'm hoping not to use another callback such as before_create to set a flag in the model or require another query into the db. Any advice is appreciated. Edit: Need to determine it in after_save hook, and for my particular use case, there is no updated_at or updated_on timestamp 回答1: I