ruby-on-rails-3.2

Override ActiveRecord << operator on has_many :through relationship, to accept data for the join model

断了今生、忘了曾经 提交于 2019-12-03 07:54:37
问题 I have three classes: Person, Position, and Directory. A Person has_many :directories, :through => :position. A Directory has_many :people, :through => :position. Both Person and Directory has_many :positions. The Position model, in addition to having an id, a person_id, and a directory_id, has one or more additional fields (e.g., title). What I would like to be able to do is add data to the join model, such as the title field, every time I add a person to a Directory.people collection. The

Easiest way to store images in database on Heroku?

你说的曾经没有我的故事 提交于 2019-12-03 07:41:00
We have a RoR 3.2 site on Heroku using their production tier PostgreSQL database. Our site users occasionally upload images of 0.5 - 2MB in size. For different reasons we want to store those images in the database, rather than as files on S3 or similar. If our site was image heavy, i.e. a Facebook clone, surely storing images in the db would not be a good thing. Anyway, it is not, so here are some of the reasons for wanting to store a users images in the database: Control over everything: S3 downtime and third party gems cannot break image handling/access to images. Easy delete: If a user

value_to_boolean deprecated; what's a good replacement?

心不动则不痛 提交于 2019-12-03 06:28:09
问题 Is there a "cool-kid-approved" replacement for ActiveRecord::ConnectionAdapters::Column.value_to_boolean in rails 3.2? 回答1: In Rails 4.2, this looks like a possible way to do it: ActiveRecord::Type::Boolean.new.type_cast_from_database(value) Which under the covers is going to do this if value == '' nil else ConnectionAdapters::Column::TRUE_VALUES.include?(value) end Or in Rails 5: ActiveRecord::Type::Boolean.new.cast(value) Which seems to end up here: def cast_value(value) if value == '' nil

Using question mark character in Rails/ActiveRecord column name

橙三吉。 提交于 2019-12-03 06:20:44
问题 In keeping with Ruby's idiom of using a question mark in boolean methods (e.g. person.is_smart? ), I'd like to do the same for an ActiveRecord field in Rails: rails generate model Person is_smart?:boolean I haven't actually run the above statement. I assume that database fields can't have a question mark in them. Will rails deal with this appropriately? Is the best practice to simply leave question marks off of models? Using Rails 3.2.8 回答1: Rails will automatically generate the method smart?

Rails controller create action difference between Model.new and Model.create

我只是一个虾纸丫 提交于 2019-12-03 06:17:13
I'm going through a few Rails 3 and 4 tutorial and come across something I would love some insights on: What is the difference between the Model.new and Model.create in regards to the Create action. I thought you do use the create method in the controller for saving eg. @post = Post.create(params[:post]) but it looks like I'm mistaken. Any insight is much appreciated. Create action using Post.new def new @post = Post.new end def create @post = Post.new(post_params) @post.save redirect_to post_path(@post) end def post_params params.require(:post).permit(:title, :body) end Create action using

Why is Google PageSpeed Insights telling me to minify javascript & CSS (using Rails 3.2 with JS & CSS compression) and how to fix?

≡放荡痞女 提交于 2019-12-03 06:15:57
I know it's not a lot I could save in KB, but to achieve a better score in Google PageSpeed Insights, and thus probably better SEO ranking, how can I fix this? From https://developers.google.com/speed/pagespeed/insights/?hl=en&url=www.tradebench.com : Minify JavaScript for the following resources to reduce their size by 2.8KiB (2% reduction). Minifying http://d2bfamm4k6zojq.cloudfront.net/…tion-ea806932c941fb875b7512a557ebead3.js could save 2.8KiB (2% reduction) after compression. It tells me the same thing for my CSS file. From my production.rb file: config.assets.compress = true config

Rails :confirm modifier callback?

不想你离开。 提交于 2019-12-03 05:54:18
I want to call a javascript function that will be conditional upon a user's response to the confirm box. For example, I have the following anchor: <%= link_to 'Sign Out', destroy_session_path, confirm: 'Are you sure that you would like to sign out?', method: :delete %> Should I just abandon the unobtrusive javascript and bind my own jQuery callback to my anchor? Or is there some magical way to pass a callback function to the Rails url helper? I did not see any discussion of callbacks in the documentation . By following the link_to source, I see that the :confirm modifier is passed to the

Rake db:test:prepare task deleting data in development database

江枫思渺然 提交于 2019-12-03 05:49:58
Using a simple Rails sqlite3 configuration example in my config/database.yml for a Rails 3.2.6 app, I used to reset my development database, re-seed it, and prepare my test database simply by performing: $ rake db:reset $ rake db:test:prepare After looking at this blog entry about testing a Rails application with Travis CI on different database engines, I thought I'd give it a try, so I installed mysql and postgresql using Homebrew (I'm on OSX Snow Leopard), set them up as per the brew info instructions. I installed the relevant gems, and configured the database and Travis files as follows:

Possible to alias a belongs_to association in Rails?

只谈情不闲聊 提交于 2019-12-03 05:25:42
问题 I have a model with a belongs_to association: class Car < ActiveRecord::Base belongs_to :vendor end So I can call car.vendor . But I also want to call car.company ! So, I have the following: class Car < ActiveRecord::Base belongs_to :vendor def company vendor end end but that doesn't solve the assignment situation car.company = 'ford' , so I need to create another method for that. Is there a simple alias mechanism I can use for associations? Can I just use alias_method :company, :vendor and

Why is my Rails mountable engine not loading helper methods correctly?

情到浓时终转凉″ 提交于 2019-12-03 04:22:48
问题 I've built a rails gem that mounts as an engine. The engine is scoped to it's own namespace. In the engine, there's an MyEngine::ApplicationHelper module which adds a bunch of view helper methods. In my application layout, I refer to some of these methods. When I first load any of the pages in development mode I get a NoMethodError , complaining that the method (defined in the gem's ApplicationHelper ) doesn't exist. Once I edit ApplicationController within my app, the problem corrects itself