Все вопросы: [activerecord]

645 вопросов

5
голосов
3ответов
8401 просмотров

Select, group and sum results from database

I have a database with some fields I'd like to sum. But that's not the big problem, I want to group those fields by the month they were created. ActiveRecord automaticaly created a field named "created_at". So my question; how can I group the result by month, then sum the fields for each month? ...

4
голосов
1ответов
2723 просмотров

How to determine if a Rails object is marked_for_destruction?

I have some objects which happen to be nested_attributes of something else. When they are marked to be deleted, Rails creates a property "marked_for_destruction". How do I read this var? Sample Yaml dump: --- &id001 !ruby/object:LineItem attributes: name:Pay created_at: 2009-10-12 16:...

3
голосов
3ответов
2089 просмотров

Validating a Rails model post-save?

I have a model with a couple of accepts_nested_attributes_for. There is a requirement that I have at least one of each of the nested attributes when saving and moving on. However, validation occurs pre-save, so when I'm removing an item and moving on, it let's it through. How can I validate th...

6
голосов
1ответов
863 просмотров

The Authlogic record method. What does this do

I came across this method called record that Ryan bates uses in his authlogic Railscast and can't seem to understand what it does. I have been through the documentation but I can't seem to follow how that helper is useful. def current_user return @current_user if defined?(@current_user) cur...

2
голосов
1ответов
4507 просмотров

"uninitialized constant ActiveRecord" on migration

have a problem creating my new table in SqlLite3 I have created this migration using the scaffolding generator: class CreateTimes < ActiveRecord::Migration def self.up create_table :times do |t| t.integer :regsite t.integer :user_id t.timestamp :added t.integer ...

192
голосов
10ответов
201146 просмотров

Rails: How do I create a default value for attributes in Rails activerecord's model?

I want to create a default value for an attribute by defining it in ActiveRecord. By default everytime the record is created, I want to have a default value for attribute :status. I tried to do this: class Task < ActiveRecord::Base def status=(status) status = 'P' write_attribute(:s...

3
голосов
2ответов
3154 просмотров

Castle ActiveRecord "Could not compile the mapping document: (string)"

Hi I am having getting an exception when trying to initialize ActiveRecord and I cannot figure out what I am missing. I am trying to convince the company I work for to use Castle ActiveRecord and it won't look good if I can't demonstrate how it works. I have work on projects before with Castle Ac...

0
голосов
1ответов
392 просмотров

Rails ActiveRecord fetch records with appropriate data types

I am using Ruby, I want data in array of hash format. I have tried out ActiveRecord's select_al method. But it returns all the data in string format, does not matter what type is its type in data base. All numbers are also returned in string format.

3
голосов
2ответов
289 просмотров

Rails ActiveRecord Table Indexes - When should they be used?

I have heard over and over that you should add indexes to any foreign key you will be doing joins on. I have also heard you should have indexes for fields you will do queries on. Does anyone have a fairly exhaustive list or set of guidelines around when and when not to add indexes? There mus...

0
голосов
2ответов
1074 просмотров

RoR ActiveRecord find_by_sql Question

I want to preface this question by stating that I am fairly new to Ruby development, however, personally, I've dedicated myself to trying to find the answers myself, as opposed to whimsically asking questions on forums. That being said, this is my first official "post", and I have made every atte...

1
голосов
2ответов
130 просмотров

Why is it bad to use models as enumerations?

I was just watching a preview of a session at Aloha on Rails called "You're Doing it Wrong". In the short preview, he mentions using ActiveRecord models as enumerations (I assume he means plugins like enumerate_by). This seems like reasonable idea to me, what are the problems? Is it just the ove...

1
голосов
1ответов
646 просмотров

Saving has_many records on create of the belongs_to record

class Bill < ActiveRecord::Base has_many :invoices end class Invoice < ActiveRecord::Base belongs_to :bill end Then in my form I've got several fields for invoices all with the name bill[invoice_names][] so I can access them from params[:bill][:invoice_names]. At the moment I've got...

6
голосов
4ответов
7867 просмотров

Update attributes unless blank?

I have an existing Project record, and I'm importing a CSV file to update the associated Project attributes. However, often the CSV will contain blank fields and I don't want to overright exisiting attributes if the related CSV field is blank. Something like this: project.update_attributes(:na...

17
голосов
2ответов
12552 просмотров

Rails: order using a has_many/belongs_to relationship

I was wondering if it was possible to use the find method to order the results based on a class's has_many relationship with another class. e.g. # has the columns id, name class Dog < ActiveRecord::Base has_many :dog_tags end # has the columns id, color, dog_id class DogTags < ActiveRec...

20
голосов
4ответов
15311 просмотров

How do rails association methods work?

How do rails association methods work? Lets consider this example class User < ActiveRecord::Base has_many :articles end class Article < ActiveRecord::Base belongs_to :user end Now I can do something like @user = User.find(:first) @user.articles This fetches me articles belongi...

0
голосов
4ответов
4316 просмотров

Rails RSpec with Multiple Databases

I run a Rails app, and we're in the process of splitting out our signup process to a separate app. The signup app has its own separate database (for CMS and collecting prospects), but it also needs to have access to the main database. This works really well using ActiveRecord::Base.establish_conn...

74
голосов
5ответов
71305 просмотров

ActiveRecord: How to get all attributes of a model that can be mass-assigned?

I would like to have a list of all attribute names that can be mass assigned. I need this for a custom form builder that will not add input fields by default that cannot be mass assigned. For example if I have the following model: class Post < ActiveRecord::Base attr_protected :account ...

1
голосов
1ответов
1024 просмотров

Calculated field in RoR (act as an ActiveRecord method)

I am building an app for cognitive tests in Rails. I have a number of tests (Quiz objects) for my visitors. In the home page I want to show only quizzes that are ready for consumption: they must have a number of questions and a number of possible answers. Of course I could query with SQL, or crea...

1
голосов
3ответов
400 просмотров

How do I define ActiveRecord relationships between two models that relate to each other in two different ways?

In my app I have the classes User, Video, and Vote. Users and Videos can relate to each other in two different ways: as a one-to-many or as a many-to-many. The former is when a User submits a Video (one user can submit many videos). The latter is when a user votes on a video (users have many...

13
голосов
1ответов
2943 просмотров

Rails' ActiveRecord serialize :attr method gives "Missing Class or module error"

I'm trying to serialize a simple attribute in an ActiveRecord model, and Rails 2.3.4 doesn't like it. class Shopper serialize :tags end >> a = Shopper.new => <#Shopper...> >>a.tags = ['aoeu','stnh'] => ['aoeu','snth'] >> a.save => TypeError: class or module ...

4
голосов
1ответов
1000 просмотров

Manually instantiate ActiveRecord models and their relationships?

If I have T-SQL (or a stored proc) that returns records from multiple tables (using DBI perhaps), is there a way for me to manually instantiate the ActiveRecord models and their associations? Obviously, I’m after database performance here. I would like to be able to build my own object hierarch...

0
голосов
3ответов
271 просмотров

Why do migrations need the table block param?

Why does the ruby on rails migration syntax look like this: create_table :my_table do |t| t.integer :col t.integer :col2 t.integer :col3 end And not: create_table :my_table do integer :col integer :col2 integer :col3 end Personally I find the second snippet m...

1
голосов
2ответов
130 просмотров

ORM that accepts SQL and simply maps the objects and relations?

I find that the use of ActiveRecord affects the way I design the database schema (though I wish it wouldn't). I'm thinking about the inefficiency of fetching data and how to reduce the overall number of queries. The find :include option can only get you so far. I come from writing stored procs...

10
голосов
1ответов
8383 просмотров

How can I create new records with has_many :through and honor :conditions?

Let's say I have a Course in which Students can enroll via a Membership (e.g. a has_and_belongs_to_many relationsip of Courses and Students). Some memberships are for students who are just observing the class (not for credit, etc.), so: class Course < ActiveRecord::Base has_many :membershi...

17
голосов
6ответов
14912 просмотров

Callback for changed ActiveRecord attributes?

I am aware of ActiveRecord::Dirty and the related methods, but I don't see a means by which I can subscribe to an attribute changed event. Something like: class Person < ActiveRecord::Base def attribute_changed(attribute_name, old_value, new_value) end #or attribute_changed do |att...

0
голосов
1ответов
147 просмотров

Showing join table field

Here I want to show the extra attribute CONFIRMED from the employments join table. What am I doing wrong? class Job < ActiveRecord::Base has_many :employments, :dependent => :destroy has_many :users, :through => :employments class User < ActiveRecord::Base has_many :employments ...

14
голосов
4ответов
6439 просмотров

How can I use ActiveRecord on a database that has a column named 'valid'? (DangerousAttributeError)

I am accessing a database that I can't change and it has a column named valid defined. Anytime I try to access an attribute, I get this exception: valid? is defined by ActiveRecord (ActiveRecord::DangerousAttributeError) The exception makes sense, but since I'm not able to change the data...

2
голосов
3ответов
564 просмотров

Partial lazy loading

I've one object User which can have multiple Posts. Example: Load the user with lazy loading on the Posts IList<User> users = User.LoadAll() Then I want to read only "half" of the users[2].Posts[3] (retrieve only the attributes that I want and not all of them from that post object), is t...

73
голосов
3ответов
69698 просмотров

Rails ActiveRecord :joins with LEFT JOIN instead of INNER JOIN

I have this code User.find(:all, :limit => 10, :joins => :user_points, :select => "users.*, count(user_points.id)", :group => "user_points.user_id") which generates following sql SELECT users.*, count(user_points.id) FROM `users` INNER JOIN `user_...

1
голосов
1ответов
149 просмотров

Castle - ActiveRecord - Inheritage

I'm trying to avoid creating the same properties in all ActiveRecord classes, so I am coding this: Have a base class where I have my common properties: Id, Version, LastUpdate, etc... public class IdentityBase<T> : ActiveRecordValidationBase<T> where T : class Then my "child" clas...