activerecord

rails3 is it possible to create a model.scope with no constraints

I am trying to create a scope on a model of mine limiting the available results to only those that are owned by the user's partner. When the user is an administrator, however, I want all models to be available. This works, but looks stupid. What is the proper rails3 way of expressing this? scope :accessible_by, proc { |user| if user.a...

What's the difference between design pattern and enterprise design pattern?

Hi, I want to know the difference between design pattern and enterprise design pattern. For example, some books call ActiveRecord an enterprise design pattern, while, singleton is a design pattern. Thanks ...

In Ruby on Rails, how can a model "has_many" and "belong_to" using a different field other than primary ID?

I am trying building a simple project from scratch using Rails 3. Usually, the models are like: class Student < ActiveRecord::Base has_many :awards end class Award < ActiveRecord::Base belongs_to :student end and we use the award.id and student.id to get the corresponding records. But what if it is class Company < ActiveRecord...

Loging Active Record save method call in Rails 3 - how

Hi, I'm looking the best way to log some of save method call. It will be save in another database. I need to log model and its attributes, and user model, which should be somehow taken from session. How to make it Rails Way? ...

Self referential association problem

Hi, I have the next models: create_table :categories do |t| t.integer :category_id t.integer :language_id t.timestamps end create_table :category_localizated_categories, :force => true do |t| t.column :category_id, :integer t.column :localizated_category_id, :integer end class Category < ActiveRecord::Base has_many :catego...

ActiveRecord association: create new association or reference existing if associated attributes match

I have two models below. They can be explained as follows: A report has a report_detail (which determines start/end months). Many reports can have the same report detail, but no two report details can be the same. class Report < ActiveRecord::Base # attr: name :: String # attr: report_detail_id :: Integer belongs_to :report_de...

Roles that are User<>Project Based

Hello... Currently I'm using Devise & CanCan which allows me to create Users with Roles using a (Roles_Users) table. That's nice, but what I want is to Have Projects in my app and for each project for a user to possibly have a role like (Admin, Viewer, etc) IE, roles are not assigned to users but to users based on what projects the...

Assigning value to serialized attribute causes SerializationTypeMismatch

I find it surprising that this has not been asked yet, so I am hoping I am doing something fundamentally wrong and help will arrive soon. I have this create_table "foo", :force => true do |t| t.text "bar", :null => false ... end class Foo < AR::Base serialize :bar, Bar end class Bar de...

Rails 3 - How to get the row number from a model with an order by

Hi, I am putting together a small app that has a leaderboard concept in it. Basically, the model is just a player_name and a current_score. what I want to do is get the ranking of a specific player, and given that new scores are coming in all the time, it needs to be dynamic. Obviously I could just do a normal find with an order by cl...

How to show validation errors from an associated model in Rails?

I have 3 models: User, Swatch + Color. A user has many swatches, and a swatch references a color. Users create swatches on their profile page (users/show/id). The color model handles validation through the swatch model with accepts_nested_attributes_for :color and validates_associated :color. My question is, how to show the color-spec...

Rails/Active Record FAILSAFE error "User can't be referred"

Using Rails 2.3.2 (not in a good situation to upgrade at the moment) with ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]. Getting the following error when trying to save when doing validations on the model, if the validations are :on => :update. If I change the validations to :on => :create and create a new record, I don't...

complex arel query

I have a model where an article can have multiple tags (and a tag multiple articles). Article has two subclasses, product and kit. Products have a category, kits have not. How can I get all articles (both kits and products) of a certain tag (I know the tag.id) , with the product's category loaded (avoiding a n+1)? ...

In Rails, what is the difference using "has_many with belongs_to" vs "has_many with has_one"?

For example, in class Student < ActiveRecord::Base has_many :awards end class Awards < ActiveRecord::Base belongs_to :student end the above should be the correct usage, but what if we use class Student < ActiveRecord::Base has_many :awards end class Awards < ActiveRecord::Base has_one :student end doesn't the above also m...

Updating mysql bit datatype using codeigniter active record

Hi, I have a table with one columns datatype as BIT(1) . I am using active record of codeigniter for performing queries. But the setting of bit is not working. Does anybody has idea about how to do it? Or I have to get back to normal query? Following is the code snippet: function itemUpdate($options=array()) { if(isset($options['isA...

Check for nil result in ActiveRecord query

I have a few places in a model that does stuff like def ServerInfo.starttime(param) find(:all, :conditions => "name ='#{param}_started'", :select => "date").first.date.to_datetime end Now, for reasons not relevant to the question, it can happen that this particular row is not in the database at all and the code above fails wit...

Force table to start ID increment from 0 again in ActiveRecord?

I am trying to setup seed data for my Rails app and am counting on the id of each model being a certain value. How can I tell the table to, after MyModel.destroy_all to start counting from 0 again instead of where it left off? ...

ActiveMailer test pass when run through ruby but not when rake test:units

Hi All - I'm having trouble with an activemailer test I created. When I run the test directly with ruby, the test passes. When I run all the unit tests they fail, because the way the message is encoded changed. Running the test from rake test:units breaks the email into multiple mimeparts, that do not exist when running ruby unit/mail...

Columns information in Active Record Models

Is there a gem or plugin, that can generate a Comments with columns names above the class definition in Active Record model? I'm pretty sure that I've seen something like this, but can't find it anywhere: Example: # columns Defs # name:string # user_id:integer # etc. class Post < ActiveRecord::Base end I could as well write something...

Efficient accessor for new_record?s in ActiveRecord relationship?

Just getting through some of the issue backlog and could use some input on best-practices for what seems like it should be a common scenario. (Rails 2.3.5) Let's say we've got form for creating posts for a blog using mass-assignment. The create action looks something like: def create if @blog.update_attributes(params[:blog]) redi...

Given the following Model, how to determine the User's Role?

Given the following model: class User < AR::B has_many :permissions has_many :projects, :through => :permissions end class Project < AR::B has_many :permissions has_many :users, :through => :permissions end class Role < AR::B has_many :permissions end class Permission < AR::B belongs_to :user belongs_to :proje...