Models not tied to DB

Is there a way I can have a model that’s basically what would be used to
simply encapsulate some data?

Here’s my situation. I have a search form that contains name fields and
two date fields outputted using date_select. I created a Search model
but don’t have it tied to ActiveRecord. I’d like to do something like
the following:

Search Controller

@search = MySearch.new(@params[:search])

Search View

date_select “search”, “from_date”, :class => “text”, :start_year =>
2007, :order => [:month, :day, :year]

Problem is, since it isn’t tied to ActiveRecord so it’s not associated
with the database, I’m not sure how I’d go about creating the model to
work properly with the html helpers. I tried creating some local
variables using write_attribute but that’s part of ActiveRecord so it
won’t work.

Any ideas?

Maybe I should generalize my question a little more. How should I
handle a date_select that isn’t tied directly to my DB model? I just
need the date select values for handling the find conditions. But
without a model, I can’t seem to set a default value on the date_select.

The B. wrote:

Is there a way I can have a model that’s basically what would be used to
simply encapsulate some data?

Here’s my situation. I have a search form that contains name fields and
two date fields outputted using date_select. I created a Search model
but don’t have it tied to ActiveRecord. I’d like to do something like
the following:

Search Controller

@search = MySearch.new(@params[:search])

Search View

date_select “search”, “from_date”, :class => “text”, :start_year =>
2007, :order => [:month, :day, :year]

Problem is, since it isn’t tied to ActiveRecord so it’s not associated
with the database, I’m not sure how I’d go about creating the model to
work properly with the html helpers. I tried creating some local
variables using write_attribute but that’s part of ActiveRecord so it
won’t work.

Any ideas?

The B. wrote:

Is there a way I can have a model that’s basically what would be used to
simply encapsulate some data?

Here’s my situation. I have a search form that contains name fields and
two date fields outputted using date_select. I created a Search model
but don’t have it tied to ActiveRecord. I’d like to do something like
the following:

Search Controller

@search = MySearch.new(@params[:search])

Search View

date_select “search”, “from_date”, :class => “text”, :start_year =>
2007, :order => [:month, :day, :year]

Problem is, since it isn’t tied to ActiveRecord so it’s not associated
with the database, I’m not sure how I’d go about creating the model to
work properly with the html helpers. I tried creating some local
variables using write_attribute but that’s part of ActiveRecord so it
won’t work.

Any ideas?

Since ruby is a duck-typed language, as long as your Search model quacks
like ActiveRecord, than everything should be fine. And I believe the
form helpers simply call the attribute as a method to get the value.

So:

<%= text_field ‘foo’, ‘bar’ %>

Will get its value from:

@foo.bar

Knowing that, we can define a class with attr_accessor’s for any
instance variables we want to make from fields out of.

I just tried this simple experiment. Seems to work for me.

#model
class Foo
attr_accessor :bar
def initialize
@bar = ‘testing’
end
end

#controller
def foo
@foo = Foo.new
end

#view
<%= text_field ‘foo’, ‘bar’ %>
yields:

Hey thanks, I hadn’t run across the attr_accessor yet. It’s almost
working now. Is creating an object via “search =
Search.new(@params[:search])” an ActiveRecord thing? I’m getting a
“wrong number of arguments (1 for 0)” on that call. If so, I’m guessing
I’ll just have to parse/set the attributes in the model manually.

Alex W. wrote:

Since ruby is a duck-typed language, as long as your Search model quacks
like ActiveRecord, than everything should be fine. And I believe the
form helpers simply call the attribute as a method to get the value.

So:

<%= text_field ‘foo’, ‘bar’ %>

Will get its value from:

@foo.bar

Knowing that, we can define a class with attr_accessor’s for any
instance variables we want to make from fields out of.

I just tried this simple experiment. Seems to work for me.

#model
class Foo
attr_accessor :bar
def initialize
@bar = ‘testing’
end
end

#controller
def foo
@foo = Foo.new
end

#view
<%= text_field ‘foo’, ‘bar’ %>
yields:

On 2/22/07, Alex W. [email protected] wrote:

form helpers simply call the attribute as a method to get the value.
Tha my not be entirely true. It depends on what you want out of it.
It’s
very difficult for example to use validation if you don’t use AR.
There are a couple of plugins you may find useful

http://www.agilewebdevelopment.com/plugins/active_form

http://www.agilewebdevelopment.com/plugins/activerecord_base_without_table

Hope that helps

The B. wrote:

Hey thanks, I hadn’t run across the attr_accessor yet. It’s almost
working now. Is creating an object via “search =
Search.new(@params[:search])” an ActiveRecord thing? I’m getting a
“wrong number of arguments (1 for 0)” on that call. If so, I’m guessing
I’ll just have to parse/set the attributes in the model manually.

Remeber that “params[:search]” is a hash itself, so you can handle this
in your initialize method of your model.

class Search
FIELDS = [
:list,
:of_attributes,
:you_want,
:as_insatnce_variables
]

attr_accessor *FIELDS

def initialize(values = {})
values.each do |name, value|
raise “Search does not have this field” unless
FIELDS.include?(name)
instance_variable_set “@#{name}”, value
end
end
end

No… super super easy to do validations, etc without a db.

As always, thanks to Rick O. for the original idea.

Alex W. wrote:

The B. wrote:

Hey thanks, I hadn’t run across the attr_accessor yet. It’s almost
working now. Is creating an object via “search =
Search.new(@params[:search])” an ActiveRecord thing? I’m getting a
“wrong number of arguments (1 for 0)” on that call. If so, I’m guessing
I’ll just have to parse/set the attributes in the model manually.

Remeber that “params[:search]” is a hash itself, so you can handle this
in your initialize method of your model.

class Search
FIELDS = [
:list,
:of_attributes,
:you_want,
:as_insatnce_variables
]

attr_accessor *FIELDS

def initialize(values = {})
values.each do |name, value|
raise “Search does not have this field” unless
FIELDS.include?(name)
instance_variable_set “@#{name}”, value
end
end
end

Excellent information by everyone who responded to my question. I’ll
give this a try (and maybe try out the activerecord base plugin that was
mentioned).

Thanks!

On 2/22/07, Brian H. [email protected] wrote:

No… super super easy to do validations, etc without a db.

http://www.bphogan.com/learn/pdf/tableless_contact_form.pdf

As always, thanks to Rick O. for the original idea.

Yes this is nice… I had seen this a loooong time ago but I had no
idea
where I saw it. Thanx for the link

This is what the active_record_base_without_table plugin does. I have
pasted the entire working code of the plugin here. The save method has
not
been implemented in this one.

module ActiveRecord
class BaseWithoutTable < Base
self.abstract_class = true

def create_or_update
  errors.empty?
end

class << self
  def columns()
    @columns ||= []
  end

  def column(name, sql_type = nil, default = nil, null = true)
    columns <<

ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default,
sql_type.to_s, null)
end
end
end
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs