Displaying 20 results from an estimated 4000 matches similar to: "Call AR belongs_to association accessor ID method on object assignment"
2009 Mar 16
4
Overriding ActiveRecord associations for special cases
Hi,
Does anyone know a good way to override AR associations to take
special cases into account. My current take is the example below which
allows the ''root'' user to access all products, but only works if I
remember to call it with a .all/.find or similar method:
''user.products.all'' instead of ''user.products''. Ideally I guess I want
to return a
2010 Jun 29
3
belongs_to. Association methods don't pass data to DB
Hi.
I have a problem with the association methods which passed to a model
through a belongs_to declaration. Here''s an illustration of the issue:
GIVEN:
# migration
class CreateArticlesAndAuthorsTables < ActiveRecord::Migration
  def self.up
    create_table :articles do |t|
      t.text     :title
      t.integer :author_id
    end
    create_table :authors do |t|
      t.text
2009 May 24
6
belongs_to not saving foreign key
Under Rails 2.3.2 using a completely brand new project, I have 2
models:
class Author < ActiveRecord::Base
  # name:string
end
class Book < ActiveRecord::Base
  # title:string
  belongs_to :author # author_id
end
And a simple test where i create a Book with an Author using the
belongs_to and then update the foreign key directly:
require ''test_helper''
class BookTest
2009 Dec 26
3
Does :class_name for belongs_to have to be a string and why?
Seemingly, when I use a symbol, I get an ActiveRecord error stating
''Can''t convert symbol into String.''  Why can''t I use a symbol here?
Possibly this is a deeper misunderstanding of symbols in general on my
part.
#ActiveRecord error
class Book < ActiveRecord::Base
  belongs_to :author, :class_name => :Person, :foreign_key
=> :author_id
end
#works
2006 Mar 19
3
Table belongs_to either of two other tables?
Suppose I have a site about books and authors, and users can leave 
comments on either a book or an author.
I''d want to have a table called "comments", which could belong to either 
an author OR a book.
In my structure I wouldn''t be sure whether to include an "author_id" or 
a "book_id", or both.
What would be the best way to handle this
2006 Apr 09
6
Write/Display AR query as Grouped Results?
I''ve got a publications table that contains an author_id foreign key and
a pubrole_id foreign key. What I want to do is query the DB using AR so
that I can get a list of all publications that belong_to a particular
author, and group the results by the pubrole.role_name (Author, Joint
Author, Editor, etc.) so that the results look something like:
Author
  book1 info
  book2 info
  etc.
2006 Mar 31
2
A.R. Associations problem
Hello,
I''m learning A.R Associations by creating a simple forum that consists 
of 3 tables: ahuthors, topics and posts.  This is the schema:
  class AddAuthorAndTopicAndPostTables < ActiveRecord::Migration
  def self.up
    create_table :authors do |t|
      t.column :username, :string
      t.column :email, :string
      t.column :created_on, :datetime
    end
    create_table
2006 Jun 13
6
Dead horse: validates_associated
Regarding validates_associated...
Let''s say I have:
  article belongs_to author
But for whatever reason, I want an article to also be written
anonymously and therefore not require an author. Then I have:
Article:
   belongs_to :author
   validates_associated :author
But I DON''T have validates_presence_of.  What I want to do is validate
that an author is valid --if it is
2006 Jan 22
2
suggest for "ambiguous column" when JOIN associated tables
Today I face with incorrect behavior in ActiveRecord.
It take place when I try to use :include parameter for .find method.
Where are two typical cases:
1. You have record with self-referential joins
CREATE TABLE keywords (id, group_id);
class Keyword < ActiveRecord::Base
  belongs_to :group,
             :class_name =>  "Keyword",
             :foreign_key =>
2005 Dec 29
3
Verification problems
Hello,
Hope this isn''t a newbie question - but I don''t seem to be able to find 
relevent information elsewhere.
I am having trouble getting the following to work:
Schema
======
ActiveRecord::Schema.define(:version => 1) do
  create_table "authors", :force => true do |t|
    t.column "name", :string
  end
  create_table "books", :force =>
2005 Apr 20
4
Error in tutorial or sintax changed?
Hi,
In this tutorial 
http://wiki.rubyonrails.com/rails/show/HowtoUseJoinsWithFindAll
it sad:
sql =<<SQL
SELECT articles.*, authors.name AS author_name
FROM articles
LEFT JOIN authors ON author_id = authors.id
SQL
and this return
*app/controllers/media_controller.rb:366: can''t find string "SQL" 
anywhere before EOF*
what is wrong? the tutorial or ruby changed?
please,
2008 Jan 10
2
Getting results matching with an array?
I have an array authors=[1,3,7] and I want to get all the posts that
match with any or all of these results.
something like...
posts = Post.find(:all, [:conditions => ''''author_id = ?'',  authors])
of course this wont work but I need something like it that works..
thanks
-- 
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
2006 Jan 22
11
ActiveRecord find
Suppose I have three tables - authors, posts and topics.
Every post have a topic and every topic has an author, so I have 
posts.topic_id and topic.author_id. When the author is guest (not 
registered), author_id is nil.
I need to find all the posts (in one query) where every post have all 
the information of the topic and the auther, so if p is one of those 
posts I could get the name of the
2009 Nov 11
4
Schema dump does not reflect column size limit
Hi,
Everything is in the title.
With Rails 2.3.4 and MySQL 5.0.41:
If have a table like this:
+----------------+------------+------+-----+---------+----------------+
| Field                       | Type       | Null | Key | Default | Extra          |
+----------------+------------+------+-----+---------+----------------+
| id                            | int(11)      | NO   | PRI | NULL    |
2009 Aug 17
1
Problem with setter override on ActiveRecord
(This message was originally written on StackOverflow -- the
formatting is much prettier there --
http://stackoverflow.com/questions/1283046/problem-with-setter-override-on-activerecord)
This is not exactly a question, it''s rather a report on how I solved
an issue with write_attribute when the attribute is an object, on
Rails'' Active Record. I hope this can be useful to others
2011 May 20
3
How to handle non model data in form?
Hey,
I got a form which looks like this:
- form_for(@article, :html => {:multipart => true}, :url =>
articles_path) do |f|
  = errors_for(@article)
  .field
    = f.label :text
    %br
    = f.text_field :text
  .field
    = f.label :author_id
    %br
    = f.text_field :author_id
  .actions
    = f.submit
But since I don''t want anyone to type in an author id I changed it
2008 Jan 15
7
PHP indexing, what's the PHP method for indexscript
Currently I have the following indexscript:
pid : unique=Q boolean=Q field=pid
postdate : field=startdate
author_name: unhtml boolean=XAUTHORNAME field=author
author_id: boolean=XAUTHORID field=authorid
url :  field=url
sample : weight=1 index field=sample
How can I create the same indexing using PHP?
With this, I can get an searchable index, but I have no idea how to set the fields, so that I
2006 May 23
5
AR Caching and Reflection
When I do this:
  term = Term.find(1, :include => :definition)
  term.definition.term
Two SQL queries are fired, why?
-- 
Posted via http://www.ruby-forum.com/.
2007 May 31
0
Rails AR/SQLServer Unit Test: [6912] failed (but getting better)
"bitsweat" has given AR/SQLServer some love, but it''s still unhappy...
http://dev.rubyonrails.org/changeset/6912
------------------------------------------------------------------------
r6912 | bitsweat | 2007-05-31 10:15:56 -0700 (Thu, 31 May 2007) | 1 line
Fix an edge case with find with a list of ids, limit, and offset. Closes #8437.
2007 Jun 02
7
I''m really bad at controllers, help please.
Hey,
Sorry for so many questions - I''m really bad at this right now.
I''m trying to cover the following code w/ rspec
  def index
    if params[:user_id]
      @user = User.find(params[:user_id])
      @messages = @user.messages
    end
  end
So basically what I''m doing is listing all the messages for a user,
provided there is an id parameter.
describe