Displaying 20 results from an estimated 53 matches for "author_id".
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 < ActiveSupport::TestCase
test "foreign key updating" do
a1 = Author.create! :name => ''a...
2006 Mar 31
2
A.R. Associations problem
...s 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 :topics do |t|
t.column :topic, :string
t.column :author_id, :integer
t.column :created_on, :datetime
end
create_table :posts do |t|
t.column :post, :text
t.column :topic_id, :integer
t.column :author_id, :integer
t.column :created_on, :datetime
end
end
def self.down
drop_table :authors
drop_table :top...
2006 Sep 21
0
post.author_id or post.author ? Rails bug?
Hi all!
I have just found something very strange.
Let''s imagine that I have posts that belong to an author.
If I do the following (the order doesn''t matter):
apost.author = Author.find(3)
apost.author_id = 2
I get a post that has both an :author_id => 2 attribute, and an
associated Author with id 3.
Then, post.save, and the author_id in the DB is 3.
I think that this is a little bit confusing... Is there something useful
in allowing to have both an author_id and an author that do not match?...
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, how I insert a text in a variable? (widout " or '' to delimite
the string)
Thank''...
2006 Jun 13
6
Dead horse: validates_associated
...nonymously 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 specified--.
+ I want to allow for the case where article.author_id = nil.
+ I DO NOT want to allow for the case where article.author_id =
SOME_INVALID_ID
During testing, I tried this:
article = Article.new
article.author_id = nil
assert c.valid? (True -- working so far)
article.author_id = 5
assert c.valid? (True -- because author 5...
2005 Dec 29
3
Verification problems
...g 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 => true do |t|
t.column "title", :string
t.column "author_id", :integer
end
create_table "catalogs", :force => true do |t|
t.column "author_id", :integer
t.column "book_id", :integer
end
Models
======
class Author < ActiveRecord::Base
has_many :book
validates_uniqueness_of :name
end
class Book <...
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 author like this:
name = p.topic.author.name
If the author i...
2010 Jun 29
3
belongs_to. Association methods don't pass data to DB
...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 :name
end
end
def self.down
drop_table :articles
drop_table :authors
end
end
# articles model
class Article < ActiveRecord::Base
belongs_to :author
end
# authors model
class Author < ActiverRecord::Base
has_many :...
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/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk...
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...
2009 Aug 17
1
Problem with setter override on ActiveRecord
...d
end
Unfortunately, this does not work. That''s what I get from console:
>> book = Book.new(:name => "Alice''s Adventures in Wonderland", :pub_year => 1865)
=> #<Book id: nil, name: "Alice''s Adventures in Wonderland", pub_year:
1865, author_id: nil, created_at: nil, updated_at: nil>
>> book.author = "Lewis Carroll"
=> "Lewis Carroll"
>> book
=> #<Book id: nil, name: "Alice''s Adventures in Wonderland", pub_year:
1865, author_id: nil, created_at: nil, updated_at: nil>
>&g...
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
2009 Nov 11
4
Schema dump does not reflect column size limit
...---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| author_id | int(11) | YES | | NULL | |
| author_bet_id | int(11) | YES | | NULL | |
| author_comment | text | YES | | NULL | |
| created_at | datetime | YES | | NULL |...
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 relationship?
Thanks in advance for any help,
Jeff
--
Posted via http://www.ruby-forum.com/.
2006 Jan 22
2
suggest for "ambiguous column" when JOIN associated tables
...oins
CREATE TABLE keywords (id, group_id);
class Keyword < ActiveRecord::Base
belongs_to :group,
:class_name => "Keyword",
:foreign_key => "group_id"
end
2. Your record has two associations with the same table
CREATE TABLE artworks (id, author_id, agent_id);
class Artwork < ActiveRecord::Base
belongs_to :author,
:class_name => "Person",
:foreign_key => "author_id"
belongs_to :agent,
:class_name => "Person",
:foreign_key => "agent_id...
2009 Dec 26
3
Does :class_name for belongs_to have to be a string and why?
...#39;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
class Book < ActiveRecord::Base
belongs_to :author, :class_name => "Person", :foreign_key
=> :author_id
end
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rub...
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 to:
.field
= f.label :author_name
%br
= f.text_field :author_name
added to article.rb:
attr_accessible :author_name
def before_validation
if...
2006 Apr 05
13
scaffold misuse?
Running ruby script/generate scaffold Product Admin
is a wonderful way to set up well, exactly what it says, a scaffold.
My problem is that I have several other tables that are linked,
through foreign keys, to the Product table. Say an "authors" table.
Now when I go enter a new product, a book, I want the application to
provide a drop down list of authors generated from the records in
2011 Aug 11
17
f.collection_select: what are the parameters?
The rails docs are so horrible on the collection_select method. They
need to show a *complete* form, and show two examples: one that calls
f.collection_select(), and another that calls collection_select(), and
explain the differences.
Given this line:
f.collection_select :topic, Topic.all, :id, :category
What the !@#$!@#$ is :topic?
--
Posted via http://www.ruby-forum.com/.
--
You received
2006 Jun 07
10
habtm "AND" find conditions
I have two tables and a join table for them
e.g. books, authors in a many to many relationship (habtm) and a join
table books_authors.
I can successfully search for a book that has
"author.id = 2 OR author.id = 4"
but I am unable to search for
"author.id = 2 AND author.id = 4"
This is because the result of all the joins only has one author.id
column so no single row has