7stud -- wrote:
> 7stud -- wrote:
>> 
>> Link.create("url" => params[:url])
>> 
>> (that assumes the links table has only one field: url)
>> 
>> Apparently when you assign a Link object to the variable @link, rails 
>> will create a record in the database corresponding to the values in the 
>> Link object.  If you look at the create method in the file 
>> LinksController.rb, that is what the create method does.  This is what I 
>> came up with:
>> 
>> @link = Link.create("url" => params[:url])
>> 
> 
> After some more testing, I discovered that you don't have to assign the 
> Link object to @link.  I think that when you create a Model object(e.g. 
> a Link object) and the Model is hooked up to a database, then as soon as 
> you create the object, rails inserts a record corresponding to that 
> object in the table.

That isn't quite right either.  You have to save an object for rails to 
enter it into a table:

mylink = Link.new
mylink.url = "www.somepage.com"
mylink.save

However, rails provides a convenience method, create(), that both 
instantiates a new object and inserts a record into the table.  create() 
takes a hash of name/value pairs as an argument, where each name is a 
field in the table.

(Or, you can pass create() an array of hashes, where each hash 
represents one record, and create() will insert multiple records at the 
same time.)





-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to