ruby-on-rails

How can I associate 2 models to another on my create action

I have 3 models with the following associations. A model Question belongs to a particular user and has many answers, an Answer model belongs to both a user and a question. A user model therefore has many questions and answers. My answer model has the fields User_id and question_id which identifies the owner of the question the answer is...

Using Rails Form Helpers with Serialized Custom Classes

Hey, I'm trying to save a hash of options in a single DB field. The form is able to save the data to the DB but not able to retrieve it again when I go to edit it (e.g. all the other fields are prepopulated except for the wp_options fields). class Profile < ActiveRecord::Base serialize :wp_options end This is my custom class: ...

Rails 3 build a select tag with has_many belongs_to association

Hi, Based on following models class Company < ActiveRecord::Base belongs_to :country end class Country < ActiveRecord::Base has_many :companies end I want to have in my companies/_form a select tag containing all the countries I think that the Company.new(params[:company]) in companies_controller#create can create the associati...

How do I fix a broken rails db migration

As a direct result of my own stupidity I have managed to somehow get my database in a broken state and can't figure out how to fix it. The problem started with a typo in one of my db migrations. I was adding a column and mis-spelled the name of the table that I wanted to add the column to. I ran 'rake db:migrate' and it failed. So I ...

How can I access "roles" in "views" in my Rails app?

In the Rails 3 app that I'm building to help me learn Ruby on (and) Rails, I'm a bit confused by the "roles/roles_users/users" tables. I have Devise and CanCan and I want to "access" these roles. Right now, I have three roles: admin staff client In the database, I have these tables (and a few others): roles (table) id => 1 || name...

Working in install acts_as_commentable, On create, error: "unknown attribute: book_id"

Hello, I'm working to install the acts_as_commentable plugin on my Rails 3 app. After adding "acts_as_commentable" to my book model, I then added a comment form on my book show view: <% form_for(@comment) do|f| %> <%= f.hidden_field :book_id %> <%= f.label :comment %><br /> <%= f.text_area :comment %> <%= f.submit "Post...

Where to put common Unit Test code in ruby on rails environment?

Using Rails 3.0, I have a small bit of code that I seem to be calling in all my Unit tests. Is there a common place to put shared unit test code that won't be incorporated into both unit and functional tests? I'd rather ONLY incorporate it into the needed files, just the unit tests. I'm new to testing practices in general. Is it common...

How do I use UTF-8 entities in a Rails view?

Rails appears to be converting the ampersand at the beginning of the utf-8 entity to an HTML entity: &amp; So &#x25B2; becomes &amp;#x25B2; but I would like to display a downward arrow instead, which is what the utf-8 entity would normally be. I'm using Rails 2.3.8 and Ruby 1.8.7. Here is what the view looks like: <%= get_arrow_fro...

ActiveRecord find with include, conditions on the include that don't affect the parent

I have two models: class Parent < ActiveRecord::Base has_many :children end class Child < ActiveRecord::Base belongs_to :parent end I want to find all parents AND their children, with conditions on the children only. BUT if the parent has no children that match that criteria, I still want the parent. I tried this: Parent.all...

Why will this DateParser not work in Safari?

I render this JSON object: [{"created_at":"2010-09-21T20:41:28Z","subject":"hello world"}] Then I use this date parser to parse it (see below), but it only works in Chrome 6.0.4, Firefox 3.6.8, but NOT Safari 5.0.2 --- I get NaN errors. What gives? Date.prototype.toRelativeTime = function(now_threshold) { var delta = new Date() - t...

IP Address Filtering

I'm looking at implementing IP Address filtering for my Rails SaaS app. In a nutshell I want administrators to be able to specify one or more IP Addresses (or a range of IP Addresses) and then my app only accept requests on their instance from the specified addresses. I'm looking at using IPAddress (http://github.com/bluemonk/ipaddress)...

Is there a way to make Rails ActiveRecord attributes private?

By default, ActiveRecord takes all fields from the corresponding database table and creates public attributes for all of them. I think that it's reasonable not to make all attributes in a model public. Even more, exposing attributes that are meant for internal use clutters the model's interface and violates the incapsulation principle. ...

How to POST files from HTML5 Drag-Drop to a Rails 3 App & Paperclip?

I'm trying to get some html5 drag-and-drop functionality in a Rails 3 app with Paperclip. So, basically: One or more files are dragged and dropped onto a DIV Files are POST'ed to a Rails action (together or one at a time) Rails action saves each files as a new attachment in Paperclip Right now the only way I can get this working is b...

rails2 to rails3 routing problem

On my old application(rails 2.3) I had: myapp::Application.routes.draw do |map| map.resources :posts, :has_many => :comments now in my rails3 app I removed the |map| syntax and I have myapp::Application.routes.draw do resources :posts, :has_many => :comments This gives me the following error: No route matches {:controller=>"comme...

Polymorphic Association with Comments - Ruby on Rails

I am having a bit of trouble getting these polymorphic associations to work completely. I followed this tutorial www.railscasts.com/episodes/154-polymorphic-association, but that seems to only work if I am path /controller/ID#/comments when making a new post. If I try to render a partial comment form right on /controller/ID# I get this e...

content_for & yield not returning any content

I'm trying to implement some javascript on a rails page using content_for and yield. I'm new to rails and learning rails3. In my applicatoin.html.erb file, I have <%= yield :javascript %> and in my index page, i've got <% content_for :javascript do %> <%= javascript_tag do %> alert('test') <% end %gt; <% end %>...

In Ruby on Rails, passing locals to a view, defaulting to true, won't work with ||=

if the code is do_more ||= true then when false is passed in, it becomes do_more = false || true and therefore will still be true. So this is one case where foo ||= default_value won't work? In this case it will need to be do_more = true if !defined? do_more ? ...

Custom/manual associations in a Rails controller

I have four models with the following example: User has one A, B, or C. User.user_type = "A" User.user_type_id = 12 In combination, the user_type and user_type_id identifies the table and record a particular user is tied to (in this example our user is connected to record #12 in table A). On the user/new form, the user decid...

could not find gem 'mysql2..' when running rails script/generate scaffold command.

I got this error: Could not find gem 'mysql2 (>= 0, runtime)' in any of the gem sources. Try running `bundle install`. when trying: rails script/generate scaffold post title:string I am on a mac osx, rails 1.8.7, 3.0 ...

RoR Passenger webserver "Extra File" errors on gem check --alien

I just installed the Phusion Passenger webserver via gem install. I then did a gem check --alien and got this error message: fastthread-1.0.7 has 3 problems .require_paths: Extra file ext/fastthread/Makefile: Extra file lib/fastthread.rb: Extra file passenger-2.2.15 has 5 problems .require_paths: Extra file ex...