Changing a column type to longer strings in rails

You should use text with Rails if you want a string with no length limit. A migration like this: def up change_column :your_table, :your_column, :text end def down # This might cause trouble if you have strings longer # than 255 characters. change_column :your_table, :your_column, :string end should sort things out. You might want :null … Read more

SQL where joined set must contain all values but may contain more

Group by offer.id, not by sports.name (or sports.id): SELECT o.* FROM sports s JOIN offers_sports os ON os.sport_id = s.id JOIN offers o ON os.offer_id = o.id WHERE s.name IN (‘Bodyboarding’, ‘Surfing’) GROUP BY o.id — !! HAVING count(*) = 2; Assuming the typical implementation: offer.id and sports.id are defined as primary key. sports.name is … Read more

New data not persisting to Rails array column on Postgres

I suspect that ActiveRecord isn’t noticing that your friends array has changed because, well, the underlying array reference doesn’t change when you: self.friends.push(target) That will alter the contents of the array but the array itself will still be the same array. I know that this problem crops up with the postgres_ext gem in Rails3 and … Read more

Creating a PostgreSQL sequence to a field (which is not the ID of the record)

Use CREATE SEQUENCE: CREATE SEQUENCE scores_job_id_seq; — = default name for plain a serial Then add a column default to scores.job_id: ALTER TABLE scores ALTER COLUMN job_id SET DEFAULT nextval(‘scores_job_id_seq’); If you want to bind the sequence to the column (so it is deleted when the column is deleted), also run: ALTER SEQUENCE scores_job_id_seq OWNED … Read more

Rails update_attributes without save?

I believe what you are looking for is assign_attributes. It’s basically the same as update_attributes but it doesn’t save the record: class User < ActiveRecord::Base attr_accessible :name attr_accessible :name, :is_admin, :as => :admin end user = User.new user.assign_attributes({ :name => ‘Josh’, :is_admin => true }) # Raises an ActiveModel::MassAssignmentSecurity::Error user.assign_attributes({ :name => ‘Bob’}) user.name # … Read more