I have a Category and a Post model, with each Post belonging to a Category. Before creating or updating a post, I need to check that the category selected exists. What's the best way to validate this information?
At the moment, I'm doing a find in the controller to ensure that the category exists. Is it possible to put these kinds of...
Hi, moved a rails app of mine onto a new server and had to install a few gem dependencies. However, after installing the mysql gem I get the error, uninitialized constant MysqlCompat::MysqlRes, whenever I try to run a rake command that involves the mysql database. It seems I only get this error when I require the mms2r gem. Has anyone ...
What are the best Ruby / Rails book available right now ?
Note : I know that this question has probably been asked before. But things change so much in the Ruby / Rails world that some of the books are not up to date or even teaches things that are deprecated in the newer release of Ruby / Rails.
...
If I have this in a migration:
t.text :body
How much text can I fit in :body? (I'm using PostgreSQL if that's relevant)
...
A while ago I asked (Joins across multiple tables with ActiveRecord with named scopes) http://stackoverflow.com/questions/1189086/joins-across-multiple-tables-with-activerecord-with-named-scopes
Now I need to create a named scope that involved joining across more than two tables, eg:
named_scope :baz_category, lambda {|c| {:joins=>([:f...
Hi, Everyone
I am working on a custom accessor method like an example below:
class Forest < ActiveRecord : Base
has_many :trees
def total_water_usage
# summarize each tree's water_usage in this forest.
# return as string
end
end
class Tree < ActiveRecord : Base
belongs_to :forest
end
That is, I need your help for...
Hi,
I have code
in view
<% uberform_for :profile, :html => { :multipart => true, :method => :put }, :url => update_avatar_path do |f| %>
<%= f.file_field :avatar %>
<p><%= f.submit 'Upload avatar' %></p>
<% end %>
in controller
def update_avatar
current_user.profile.update_attribute(:avatar, params[:avatar])
redirect_...
Hello everyone. I'm currently creating a Rest client for making blog posts much in the spirit of pastie.el. The main objective is for me to write a textile in emacs and make a post to a Rails application that will create it. It is working fine until I type anything in either spanish or japanese, then I get a 500 error. pastie.el has this...
I have joined a rails project as a contractor. The project has been going for more than a year. The code is written by about 10 different developers and most of them are contractors as well. They have different code style. Some of them came from Java. The code has horrible scores with metric_fu. Many functions are very long (100 - 300 li...
In my RSpec tests, I need to simulate an AJAX GET request to the index action, and have been using the code as described in both the Rails docs and the RSpec book:
xhr :get, :index
This always fails though, as the test tries to load the show action (without any parameters) rather than the specified index action.
The controller action...
OK So I've got my models like so:
Team
has_many :users, :through => memberships
User
has_one :user_status
Now in my Team model I want to be able to find the maximum 'updated_at' value of my 'user_status' models.
I tried doing something like:
Team
has_many :users, :through => :memberships
has_many :user_statuses, :through =>...
ActiveSupport offers the nice method to_sentence. Thus,
require 'active_support'
[1,2,3].to_sentence # gives "1, 2, and 3"
[1,2,3].to_sentence(:last_word_connector => ' and ') # gives "1, 2 and 3"
it's good that you can change the last word connector, because I prefer not to have the extra comma. but it takes so much extra text: 44 ...
I have a table with a compound index that wasn't created through a rails migration. Now, I need to create a rails migration that will delete this index and create a new one, but I don't necessarily know what the name of the index will be.
I know that it is possible to get a list of table names and column names within a migration step. I...
I've got a massive script with about 20 embedded if statements (Yay!) that is used to parse through a data file. And in a sense, that's correct because the script should not continue operating if any of those evaluations fail.
But my gut says there's a more elegant way to accomplish the same thing. I'm familiar with the statemachine plu...
Hi,
I have a contact form on my site that operates with a rails remote_form_for. After someone submits the form, the html in that div gets replaced with a success message instead of the entire page reloading (the reasons for doing this aren't too relevant...but basically this contact form is way below the fold and I don't want the page...
I have the code sequence below in a rails controller action. Before the IF, params contains the request parameters, as expected. After it, params is nil: can anyone please explain what is happening here?
if false
params = {:user => {:name => "user", :comment => 'comment'}}
end
Thank you.
...
I tried the auto_complete text field in rails 2.3.3 and the server says it denied request because of no authenticity token. I can see that the helper doesn't automatically create a parameter for it.
How I can manually do this? And I do not want to disable forgery prevention for this autocomplete.
...
I created a sortable list in my RoR project, unfortunately it's not saving the list position. Upon page refresh the items return to their normal spot. I've pasted the code below or you can git it: git://github.com/mdgrech/23notes-.git
app/views/notes/index.html.erb
////////////////////////////////////////////////////////////////////////...
I've got my first RoR app deployed to Dreamhost and it's using Passenger. The one note on Dreamhost's wiki about slow response mentioned changing a RewriteRules line in the public/.htaccess file to use FastCGI. But I assume this will have no effect if I'm using Passenger, is that right?
I've looked at the logs and compared them to my lo...
I have no problems with running it in development mode via rake jobs:work. However, I'm somehow unable to figure out how to use it in production. I'm using Capistrano for deployment.
Thanks for any advice!
...