ruby-on-rails

How do Rails migrations enforce relationships?

When you run: rake db:migrate the only files that are being processed are those in db/migrate/ right? Well since relationships such as one-to-one, one-to-many and many-to-many are defined in the in app/models/ , how does Rails enforce such relationships? After I do my migration and look at the generated database schema, I can't see a...

How do you spec out "read-only" behavior of a Model?

For example, let's say I have a Question model, that has the boolean fields answered and closed. How would I test the behavior that a Question should be read only when marked as answered using RSpec? This seems like it's the behavior of the model, but I'm not sure how to best test it. Should I be using a before filter for this behavio...

Add shortcut methods or follow ActiveRecord semantics?

Yet another argument with a friend of mine. Consider this code: class User < ActiveRecord::Base has_many :groups def in_group?(group) groups.include?(group) end end class Group < ActiveRecord::Base has_many :members def add_user(user) members << user end end My opinion is that these methods add extra unnecessar...

Why did Ruby on Rails deprecate the scaffold method

I am learning Ruby on Rails so I'm sure I'll find this out sooner or later. Why would the scaffold method be deprecated in version 2 of Rails? ...

Is it slow to use a redirect because it causes an additional request?

In a Rails app sometimes you use a redirect in an action... redirect_to :controller => 'sessions', :action => 'new' I wonder if that's bad though because it sends back a 302 status to the browser and then the browser makes a whole new request. It's an additional back-and-forth. Would it be better to just render a template? render :t...

Thread problem with Druby and ActiveRecord

Hi All, I have a rails app and a separate druby process. This process gives me some methods, and at the first line of each druby's method there is a call to ActiveRecord::Base.establish_connection, where the db_name depends on a param set by the rails application. Sometimes the process is getting the wrong database name and I think it ...

Ruby on Rails - Capistrano and SVN on Windows. Setup help

I have Subversion setup and running on my local network with windows and svnserve. I'd like to use Capistrano to deploy to a remote ubuntu server but am a bit on unsure on what I need to do to get the client end to work on windows. My understanding is that the remote server needs to be able to tunnel into my subversion server. I've re...

select_tag is sorting (strangely) [Rails]

I have a select box that looks like this (within a form_for) <%=f.select(:whatever_id, {"blah"=>0, "blah2"=>1, "blah3"=>2, "blah4"=>3}, {:include_blank => true}) %> and the output is good, but weird... like this: <select id="personal_information_whatever_id" name="personal_information[whatever_id]"><option value=""></option> <optio...

Why is this controller test on a create action failing?

I'm getting a failing test here that I'm having trouble understanding. I'm using Test::Unit with Shoulda enhancement. Action in users_controller.rb I'm trying to test... def create unless params[:user][:email] =~ / specific regex needed for this app /i # ... render :template => 'sessions/new' end end Test... context 'on...

Re-using model in multiple applications

Due to hosting constraints I am porting an ASP.NET MVC to Ruby On Rails. In my ASP.NET application I had 2 web applications. One for the Admin section, and one for the public section. Both referenced a class library that held all my Business Logic and Data. Now I want to accomplish the same thing in Ruby On Rails. How do I use my mod...

How can I set the Rails environment for my somewhat stand alone Ruby script?

I have a Ruby script in my Rails app that I use to load some data from Twitter. In the future I will make it an automatic background process, but for now I run it manually like: ruby /lib/twitter/twitterLoad.rb In order to use the Rails model classes and such, I have the following as the top line of the script: require "#{File.dirn...

How do you specify POST params in a Rails test?

Working with Test::Unit and Shoulda. Trying to test Users.create. My understanding is that Rails forms send params for an object like this: user[email] Which turns into hash in your action, right? params[:user][:email] OK, so in my test I've tried... setup { post :create, :post => { 'user[email]' => 'invalid@abc' } } and setup ...

OpenID delegation seems not to work with OpenID Authentication plugin for Rails

I've just installed the OpenID Authentication plugin because I'd like to use it for my projects, but as I tried to login using my blog url I got the first error message: "Sorry, the OpenID server couldn't be found" Does the plugin support delegation? If yes, what is wrong with my blog? (I use wp-openid with XRDS-Simple) Thanks! ...

Non-working Rails link in Firefox

I have the following Rails link generating code (I have removed potentially 'industry secret' stuff, sorry for the odd names, but the length of variable names and values match) <%= link_to_remote "FOUR", :method => "get", :url => {:action => "testing01_four_log_info", :fourth_name => "LA1", :testing01_num => "123"} %> This code gener...

ActionMailer problem- what is the correct syntax for sending PDF attachments

What is the correct syntax for sending an email with actionmailer that includes some PDF file attachments? I am using Gmail for SMTP, with the TLS plugin. Here is what I have so far (and have tried variations on this too): **lead_mailer.rb:** def auto_response(lead) recipients lead.email subject "Information" body ...

Using Msql views in a Ruby on Rails app to improve performances

I'm having some performances issues in a rails project (running on rails 2.0.5), for example in my user administration pages. my user model has many relations (details, addresses, roles...) who get loaded with eager loading. That creates really huge SQL queries, for some cases, it takes almost a minute to load 30 users. On the other han...

Best Way to Modify/Format Database Data in the Controller?

Let's say that in the controller I get an array of objects from the database this way: @statuses = TwitterStatus.find(:all, :order => "tweet_id DESC", :include => :twitter_user) Also I have the following loop in the view: <% unless @statuses.nil? -%> <ol> <% for status in @statuses %> <li><%= h(status.text -%>/li> <% end -%> </ol...

Reimplement ASP.NET Membership and User Password Hashing in Ruby

I have a large database of users (~200,000) that I'm transferring from a ASP.NET application to a Ruby on Rails application. I don't really want to ask every user to reset their password and so I'm trying to re-implement the C# password hashing function in Ruby. The old function is this: public string EncodePassword(string pass, strin...

Fill select dropdown based on selected item

I'm working on a rails app. I have a dropdown with countries. Selecting a country would fill the city dropdown with the cities in that country. I know how to do this server-side, but I'd like to do it client-side if possible. I've used the RJS helpers, but I've no experience with jQuery (not exactly even sure what it is) or the other...

What's the correct way to complete a Rails create action?

I have a User.create action that provisionally registers a new user and sends an email with a generated password. What is the most Rails-like way to complete that action? I want to do everything exactly right from now on. No more nonsense. This time I'm serious. I'm thinking these are the options... Create a view called login_email_se...