1

I wrote a little ruby script, which connects itself to a mysql database and creates a table (if this table doesn't exist yet). After this the script should store content in this table I try to store this data using:

Table.create(:foo => "bar", :foobar => "something", :blallala => "blololl") 

I also tried

Table.new(:foo => "bar", :foobar => "something", :blallala => "blololl") 

but it seems to do the same because I always get the error:

Mysql::Error: Table 'my-username.my-dbname' doesn't exist: SHOW FULL FIELDS FROM table-name

SO this is what I got so far:

 ActiveRecord::Base.establish_connection(
                :adapter => "mysql",
                :host => "localhost",
                :username => "my-username",
                :password => "my-password",
                :database => "my-db-name",
                :encoding => "UTF8"
        )

        table_name = "my_table"
        unless ActiveRecord::Base.connection.tables.include? table_name
                ActiveRecord::Schema.define do
                        create_table :"#{table_name}" do |table|
                                table.column :foo, :string
                                table.column :bar, :string
                                table.column :blallala, :string
                        end
                end
        end

        class Table < ActiveRecord::Base
                self.table_name = "#{table_name}"
        end

         Table.create(:foo => "bar", :foobar => "something", :blallala => "blololl")
         #Table.new(:foo => "bar", :foobar => "something", :blallala => "blololl")

So the question is: How do I actually create a columo/row and why does Table.create(:foo => "bar", :foobar => "something", :blallala => "blololl") not work?

4
  • So, what's the question here? Commented May 5, 2012 at 13:26
  • The question is: How do I actually create a columo/row and why does Table.create(:foo => "bar", :foobar => "something", :blallala => "blololl") not work? Commented May 5, 2012 at 13:39
  • You can't create a table and at the same time insert data to it. It has to be two separate statements. So it's no wonder your first two samples don't work. Commented May 5, 2012 at 13:42
  • well I doesnt not even work when I insert sleep(42) before the Table.create or what do you mean by that? / What would be the correct way? Commented May 5, 2012 at 13:47

1 Answer 1

1

This worked for me:

# establish connection here

class Table < ActiveRecord::Base
  self.table_name = "the_table"
end

unless Table.table_exists?
  ActiveRecord::Schema.define do
    create_table :the_table do |table|
      table.column :foo, :string
      table.column :bar, :string
      table.column :blallala, :string
    end
  end
end


Table.create(:foo => "bar", :bar => "something", :blallala => "blololl")
Sign up to request clarification or add additional context in comments.

2 Comments

Ah found the error (it was a typo) fixed it and it's working thanks dude :3
No problem. Pay more attention next time :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.