ruby-on-rails

How to set access-control-allow-origin in webrick under rails?

I have written a small rails app to serve up content to another site via xmlhttprequests that will be operating from another domain (it will not be possible to get them running on the same server). I understand I will need to set access-control-allow-origin on my rails server to allow the requesting web page to access this material. It ...

Rails: find by day of week with timestamp

I need to grab the records for same day of the week for the preceeding X days of the week. There must be a better way to do it than this: Transaction.find_by_sql "select * from transactions where EXTRACT(DOW from date) = 1 and organisation_id = 4 order by date desc limit 7" It gets me what I need but is Postgres specific and not very "...

has_many :through on self?

I have a User model. A user can be either a dj, a club, or a clubber (this is controlled by the User#account_type attribute). A club can have many djs, and a dj can have many users: enumerated_attribute :account_type, %w(^clubber dj club), :nil => false do label :clubber => "Clubber" label :dj => "DJ" label :club => "Club" end ...

Restricting Edit and Delete

I want to be able to edit and delete resources myself, but not allow users of the application to do so. Is there an easy way of doing this in Rails? An incomplete solution would be just to remove the "delete" and "edit" buttons from the index view, but that doesn't disable their ability to do so via direct HTTP requests. Running Rails...

Trouble with Rails has_many relationships

I'm writing an app where a user can both create their own pages for people to post on, and follow posts on pages that users have created. Here is what my model relationships look like at the moment... class User < ActiveRecord::Base has_many :pages has_many :posts has_many :followings has_many :pages, :through => :followings, :source ...

Configure Cucumber with SentientUser

I have an application using the SentientUser gem to provide the current user to my models. On top of that I'm using default scoping to ensure that a user can only ever access data that they own. So a typical model for me looks something like this: class Location < ActiveRecord::Base validates_presence_of :name, :time_zone, :address ...

Rails, if instance is in a scope?

I'm using rails 3 and I can't seem to check if a given instance is in a scope, see here: p = Post.find 6 +----+----------+-------------------------+-------------------------+-------------------------+-----------+ | id | title | publish_date | created_at | updated_at | published | +----+----------+...

is it possible to combine will_paginate with find_by_sql?

In my rails application I want to use will_paginate plugin to paginate on my query. Is that possible? I tried doing something like this but it didn't work: @users = User.find_by_sql(" SELECT u.id, u.first_name, u.last_name, CASE WHEN r.user_accepted =1 AND (r.friend_accepted =0 || r.friend_accepted IS NULL) ....

Ruby on Rails one-to-many relationship

I would like to model a betting system relationship using the power of Rails. So let's start with doing something very simple modelling the relationship from a user to a bet. I would like to have a model Bet with two primary keys. Here are my migrations: class CreateBets < ActiveRecord::Migration def self.up create_table :bets do...

How to make activerecord use a field generated on the fly by find_by_sql

I'm using find_by_sql with Activerecord which I generate another field there that doesn't in the original table as a combination of different fields like: select (field1 + field2) as new_field_name If I try to access the newly generated field like: @user.new_field_name I get nothing! How do you suggest I should approach this proble...

Download and Open Compressed SQLite3 Database on Heroku

I have a script file for parsing through a SQLite database. I now need to create a CRON job that will download and unzip said database from a third-party (already hosting as SQLITE). I understand this can be done using WGET and UNZIP, but given Heroku's read only file system, is this possible entirely in memory? Thanks. ...

ruby subclass filter

Hey! Maybe I am getting the idea of a subclass wrong, but I have a Person model and it has an attrib called "age" so Person.first.age #=> '20' Now I want to have a model that's basically persons 55 or older so I know I can have a class like this: class Senior < Person end But how can I "pre-filter" the Senior class so that every ob...

Singleton Objects causing error in session: singleton can't be dumped in Ruby

Im using Ruby on Rails and im adding an array of users into a session object. The problem is when I add it to the session I get the following error Status: 500 Internal Server Error singleton can't be dumped The problem is my user class called Expert is not a singleton class. The following is a snippet of my controller code. if @exp...

export to excel,pdf and doc using Ruby on Rails

Could you please help me in exporting files to excel,pdf and .doc format through rails applications?? ...

Validate a belongs to association in a build situation.

I have a Mission model that has_many Task, and the Task belongs_to Mission For security I've made this validation on the Task Model: validates_presence_of :mission_id validates_numericality_of :mission_id But the problem is that when create a Mission and add tasks like this: @mission.tasks.build The validation returns error,...

Rails single table inheritance/subclass find condition in parent

Hi all, I have a table called Users (class User < ActiveRecord::Base) and a subclass/STI of it for Clients (class Client < User). Client "filtering" works as expected, in other words Client.find(:all) works to find all the clients. However, for users I need to filter the result to only find users that are NOT clients (where type is nu...

Rails LinkedIn gem only gets first name and last name out of profile

I'm currently trying to use this gem http://github.com/pengwynn/linkedin. Everything works fine (i.e. authentication and calling the method which gets the profile), except that the profile object only contains the first name and the last name. I'm trying to get my own profile info through the api, so the necessary information is there....

Profile memory-performance for part of an rails project

I want to test the profile usage of an important library-class of my rails-project. It uses ActiveRecord so I need all rails dependencies to profile it. As far as I know, I need a patched ruby (rubygc) so script/profile and script/benchmark can track memory usage. I tried to follow this official guide to patch the source code of ruby 1....

[].include?(:test) return nil [RoR]

Having the strange problem that [].include?(:test) returns nil instead of expected false. The whole thing only happens when running the app (i see it in rubymine debug mode), not in irb or rails console. I tested following [].include?(:test) # nil [].include?(:test).nil? # nil [].class # Array Seems as if include? is overwritten so...

RMagick returns nil reading JPEG

When I try to open a JPEG format file in Rails using RMagick, it always return nil with any jpg file. Other file formats open well. $ script/console Loading development environment (Rails 2.3.4) >> require 'RMagick' >> img = Image.read("1.gif").first => 1.gif GIF 230x100 230x100+0+0 PseudoClass 256c 8-bit 2kb >> img = Image.read("1.png"...