All Questions

Tagged with
Filter by
Sorted by
Tagged with
213 votes
4 answers
113k views

Rails :dependent => :destroy VS :dependent => :delete_all

In rails guides it's described like this: Objects will be in addition destroyed if they’re associated with :dependent => :destroy, and deleted if they’re associated with :dependent => :...
Sergey's user avatar
  • 4,732
129 votes
6 answers
144k views

Removing a model in rails (reverse of "rails g model Title...")

rails g model Rating user_id:integer message:string value:integer How can I completely remove this model? Thanks
Colbern's user avatar
  • 1,291
126 votes
8 answers
117k views

Using helpers in model: how do I include helper dependencies?

I'm writing a model that handles user input from a text area. Following the advice from http://blog.caboo.se/articles/2008/8/25/sanitize-your-users-html-input, I'm cleaning up the input in the model ...
O. Frabjous-Dey's user avatar
110 votes
9 answers
111k views

Rails: Validating min and max length of a string but allowing it to be blank

I have a field that I would like to validate. I want the field to be able to be left blank, but if a user is entering data I want it to be in a certain format. Currently I am using the below ...
bgadoci's user avatar
  • 6,433
95 votes
10 answers
94k views

Rails: Update model attribute without invoking callbacks

I have a User model that has a :credits attribute. I want a simple button that will add 5 to the user's credits, through a route called "add" so that /users/3/add would add 5 to the credits of user ...
Sam Stern's user avatar
  • 24.9k
84 votes
5 answers
58k views

How to access URL helper from rails module

I have a module with a function. It resides in /lib/contact.rb: module Contact class << self def run(current_user) ... end end end I want to access the URL helpers like '...
sizzle's user avatar
  • 2,282
68 votes
3 answers
26k views

How to know whether a model is new or not?

class Post < ActiveRecord::Base end post = Post.new How do I judge whether the 'post' is a new model which is not pulled from the database?
Croplio's user avatar
  • 3,503
58 votes
3 answers
78k views

Ruby on Rails: errors.add_to_base vs. errors.add

I have read that errors.add_to_base should be used for errors associated with the object and not a specific attribute. I am having trouble conceptualizing what this means. Could someone provide an ...
Tony's user avatar
  • 18.9k
51 votes
8 answers
25k views

Rails Model has_many with multiple foreign_keys

Relatively new to rails and trying to model a very simple family "tree" with a single Person model that has a name, gender, father_id and mother_id (2 parents). Below is basically what I want to do, ...
Kenzie's user avatar
  • 1,285
42 votes
5 answers
19k views

guidelines for where to put classes in Rails apps that don't fit anywhere

I'm wondering if there are any best practices about where to put non-standard Ruby files in Rails apps, those that don't fit in any of the default directories (controllers/models etc.). I'm talking ...
Kuba Suder's user avatar
  • 7,677
41 votes
7 answers
48k views

Rails Model without a table

I want to create a select list for lets say colors, but dont want to create a table for the colors. I have seen it anywhere, but can't find it on google. My question is: How can I put the colors in ...
user993460's user avatar
36 votes
4 answers
28k views

Rails: Serializing objects in a database?

I'm looking for some general guidance on serializing objects in a database. What are serialized objects? What are some best-practice scenarios for serializing objects in a DB? What attributes do you ...
keruilin's user avatar
  • 17.2k
31 votes
2 answers
14k views

Rails put validation in a module mixin?

Some validations are repetitive in my models: validates :name, :length => { :minimum => 2 }, :presence => true, :uniqueness => true validates :name_seo, :length => { :minimum => 2 },...
Victor Pudeyev's user avatar
30 votes
3 answers
5k views

problem with passing booleans to update_attributes

I've got the following Model: class GuestCatering < ActiveRecord::Base # Validation validates :name, :presence => true validates :order_number, :presence => true validates :...
LeonS's user avatar
  • 2,704
24 votes
3 answers
20k views

Rails: Modifying a Model Generated by Scaffolding

How do you modify a model you've generated using modeling? For example, the model myModel originally had columns a, b and c, but I now want to add column d.
Andrew Hampton's user avatar
23 votes
4 answers
7k views

Rails - Where (directories) to put Models that are not Active Record

We are building out apps that have Models that are not database components. We are curious to learn what others are doing in the rails community to address this subject. We are struggling with where ...
Michael Durrant's user avatar
20 votes
5 answers
39k views

Ruby on Rails - Access controller variable from model

I am trying to access an instance variable which is set in the controller in the model. The controller is the products controller and the model is the products model. The instance variable is a ...
RailsSon's user avatar
  • 20.3k
20 votes
4 answers
16k views

How to override a column in Rails model?

I have a model for one of my database tables. I want to override the column name for that particular table. How would I achieve it. For example, let my table be called DUMMY and it has one column ...
bragboy's user avatar
  • 35.3k
17 votes
2 answers
19k views

Roll back all rails migrations or drop tables and modify migrations (start from scratch)

I'm new to Rails and have started a project that I'm unhappy with my models and db schema. I'd like to start again from scratch but keep all my views, controllers. What's the best way to go about ...
AdamT's user avatar
  • 6,475
17 votes
3 answers
6k views

Namespaced models in Rails: What's the state of the union?

Since the beginning, Rails has had issues with namespaced models. As time went on, pretty much everybody gave up on using it. Myself included. With Rails 2.3 out, I'd like an update on the situation. ...
kch's user avatar
  • 78.5k
16 votes
4 answers
16k views

Rails form validation conditional bypass

I have a rails model that validates uniqueness of 2 form values. If these 2 values aren't unique the validation errors are shows and the "submit" button is changed to "resubmit". I want to allow a ...
hacintosh's user avatar
  • 3,822
15 votes
6 answers
11k views

What is purpose of around_create callback in Rails Model?

when is around_create callback code executed, in what situations we should use it?
Maddy.Shik's user avatar
  • 6,707
15 votes
2 answers
29k views

Helper methods for models in Rails

Is there a proper place for helper methods for models in Rails? There are helper methods for controllers and views, but I'm not sure where the best place to put model helper methods. Aside from adding ...
at.'s user avatar
  • 51.9k
14 votes
2 answers
2k views

Are there any example Rails apps where business logic is kept in separate classes than ActiveRecord?

There's been a recent surge in the Ruby community of blog posts, tweets, and comments on the benefits of using better OO design, and specifically separating business logic from persistence logic. ...
Ben Crouse's user avatar
  • 8,330
12 votes
4 answers
8k views

Ruby on Rails model inside namespace can't be found in controller

I'm new to rails and can't figure out this issue... I have a controller Admin::Blog::EntriesController defined in app/controllers/admin/blog/entries_controller.rb And I have a model called Blog::...
user avatar
12 votes
2 answers
5k views

How to protect a Rails model attribute?

My Invoice model has an address_id attribute, and I don't want this address_id to change FOREVER. So I don't want this to happen outside the class: invoice.address_id = 1 invoice.address = ...
gsmendoza's user avatar
  • 385
12 votes
3 answers
14k views

Ruby on Rails: Properly creating a model instance that has relation to multiple models

Let's say I have a standalone User model and Service model. I also have a Order model that holds the orders created by an user for a service. I'm wondering how I could properly create an order entry ...
Srini K's user avatar
  • 3,355
12 votes
1 answer
5k views

Ruby on Rails ActiveRecord: pluralization

I'm new to Rails, so forgive my ignorance of ActiveRecord. One of my models is named "campus". I ran the migration and it pluralized everything except "campus". I thought that was lame so I added ...
Donald Hughes's user avatar
12 votes
2 answers
1k views

has_one, :through => model VS simple method?

I have some issues using has_one, through => model. The best is to show you my case. class Category has_many :articles end class Article has_many :comments belongs_to :category end class ...
Hartator's user avatar
  • 5,127
11 votes
4 answers
6k views

How do you model "Likes" in rails?

I have 3 models: User, Object, Likes Currently, I have the model: a user has many Objects. How do I go about modeling: 1) A user can like many objects 2) an Object can have many likes (from ...
user1099123's user avatar
  • 6,405
10 votes
5 answers
5k views

Rails: Skinny Controller vs. Fat Model, or should I make my Controller Anorexic?

I know similar questions have been answered before - such as: Where should logic go where to do certain tasks, etc. But I have a more specific question - How far should I take this axiom: keep ...
konung's user avatar
  • 6,958
10 votes
1 answer
6k views

rails how to properly move models to a namespace

I'm running rails v 3.2 . After some time developing application I need to move some models into namespace ex: /app/models/address.rb to /app/models/local/address.rb I tried simply moving models to ...
Elmor's user avatar
  • 4,835
9 votes
2 answers
28k views

How do you pass data from a controller to a model with Ruby on Rails?

How do you pass data from a controller to a model? In my application_controller I grab the user's location (state and city) and include a before_filter to make it accesible in all my controllers via ...
Laser's user avatar
  • 5,259
9 votes
2 answers
6k views

booleans in rails with sqlite

I am a bit of a noob still with rails, but I am running across something that seems a bit odd. I added a boolean field to a model in the database thusly t.column :admin, :bool, :default => false, :...
Matt Briggs's user avatar
  • 41.7k
9 votes
3 answers
9k views

call before methods in model on ruby

This my implementation to developing way to run code before all method in your model The call "before_hook :months_used" method need to be on bottom of class to the ExecutionHooks can get the ...
Breno Perucchi's user avatar
9 votes
3 answers
3k views

Is subclassing a User model really bad to do in Rails?

I am getting lots of push back from Rails because I have subclassed User into many different subclasses. In my application, not all users are equal. There's actually a lot of model objects and not ...
Fire Emblem's user avatar
  • 5,961
8 votes
4 answers
11k views

Why aren't Rails model attributes accessible using symbols instead of strings?

I need to compare some Rails (2.3.11) model attribute values before and after a database update, so I start by finding my record and saving the existing attribute values in a hash, as follows: id = ...
Dexygen's user avatar
8 votes
2 answers
10k views

How can I use a named scope in my model against an array of items?

I know that I can do a query for recent books based on an array as in scope :recent_books, lambda {|since_dt| {:conditions=>{:created_at >= since_dt}}} but how can I do a similar query when I ...
Michael Durrant's user avatar
8 votes
1 answer
15k views

can't write unknown attribute `scrapbook_entry_id'

Attempting to add data to a join table of scrapbook_entries which has_one :scrapbook and has_one :recipe. :recipe and :scrapbook already exist. I am trying to add them to link them with the ...
MikeHolford's user avatar
  • 1,871
8 votes
3 answers
7k views

How can default values in Sequel Models be set?

Given the code below, how can default values be defined for the Model. (let's say the default for :name should be 'Thing'). require 'pp' require 'sequel' DB = Sequel.sqlite DB.create_table :items ...
user avatar
8 votes
2 answers
3k views

What is the "rails way" to access a parent object's attributes?

Let's say I have a model Doctor, and a model Patient. A Patient belongs_to a Doctor. A Doctor has an attribute office. I would want to, given a Patient p, be able to say p.office and access the ...
Tim's user avatar
  • 6,159
8 votes
3 answers
10k views

Rails: accessing field value from model method

Just started learning Rails (3). I am tearing my hair out trying to find how to do something presumably utterly trivial: access the value of a model instance's field, from inside a method on that ...
jameshfisher's user avatar
  • 35.8k
7 votes
4 answers
15k views

Rails Model: Name -- First, Last

I'm fairly new to rails, working on a Rails 3 app with a Profile model for users. In the profile Model I'd like to have a "name" entry, and I'd like to be able to access logical variations of it ...
Andrew's user avatar
  • 42.8k
7 votes
2 answers
11k views

Use a Rails helper in a model [duplicate]

Possible Duplicate: Access a view helper for a model in rails I know this is probably not something I should do often, but I want to use a helper within a model. I'm generating a spreadsheet ...
Joe Flynn's user avatar
  • 7,076
7 votes
5 answers
8k views

Rails 3 - how to skip validation rule?

I have for the registration form this validation rule: validates :email, :presence => {:message => 'cannot be blank.'}, :allow_blank => true, :format => { :with =&...
user984621's user avatar
  • 47.4k
7 votes
4 answers
26k views

Rails 3 find all associated records has_many :through

I would like to list all posts that are connected with some specific category and classroom. I have: class Post < ActiveRecord::Base has_many :category_posts has_many :categories, :through =&...
Sergey's user avatar
  • 4,732
7 votes
2 answers
5k views

Ruby NoMethodError - undefined method `blah_url' for BlahController

I am calling this js from a link: function createNewTopLevelEntry(){ var user_id = $("#user").val(); var header = prompt("Enter the name"); $.ajax( '/users/' + user_id + '/entries', { data: { ...
Maks Demin's user avatar
7 votes
2 answers
8k views

Testing validations in model using RSpec with Rails

I'm super new to testing my app using RSpec and I'm trying to test the validation of a comment without a user and keep getting syntax errors. Here is the comment model code. class Comment < ...
Jerry Hoglen's user avatar
7 votes
3 answers
3k views

Where do I put my database query tests in rails?

I am coming from a Spring/hibernate background. I have noticed that Rails has no dao and service layers. This really speeds up development, but I don't know where to put my tests sometimes. Right now,...
egervari's user avatar
  • 22.4k
6 votes
6 answers
16k views

Reverse order of display of blog entries and comments, Ruby on Rails

I am new to rails so could use some help here. I have followed several tutorials to create a blog with comments and even some of the AJAX bells and whistles and I am stuck on something that I hope is ...
bgadoci's user avatar
  • 6,433

1
2 3 4 5
23