Method "movie_file=" can someone explain this to me please?

I’m using this method, adapted from a similar one for pictures, and it
works fine but i don’t really understand how:

def movie_file=(input_data)
self.filename = input_data.original_filename
self.content_type = input_data.content_type.chomp
self.file_data = input_data.read
end

i’m using the new fields this creates, such as filename, content_type
etc, in my other methods, and it all works, but what’s the meaning of a
method ending in an equals sign like this? I’ve had errors with other
classes where it says things like “missing method ‘story=’” in my story
controller (or maybe it was the model).

I’m guessing that this is a standard rails way of doing something but
it’s got me puzzled…at a guess, i’d say that it’s like an add-on to a
constructor, that gets called after the constructor is called, to add
some new fields to the object? Is that right?

thanks
max

On 08 Sep 2007, at 10:37, Max W. wrote:

classes where it says things like “missing method ‘story=’” in my
story
controller (or maybe it was the model).

It’s an assign method.

def movie_file
#code
end

The above is used to GET the movie file

def movie_file=(x)
#code
end

The above is used to SET the movie file

I’ll simplify a little, but suppose you don’t have a field movie_file
in your model (i.e. database table), you can use this method to
pretend it’s there. You then handle it with the variable “x” so that
it does something useful.

Best regards

Peter De Berdt

On Sep 8, 1:37 am, Max W. [email protected]
wrote:

etc, in my other methods, and it all works, but what’s the meaning of a
max

Posted viahttp://www.ruby-forum.com/.

att_accessor is a way of defining “virtual” AR attributes or virtual
columns in a table, the canonical example being “password” and
“password_confirm” which are taken from a user’s registration form but
never saved to database (the hashed password and salt are saved).

Peter De Berdt wrote:

On 08 Sep 2007, at 10:37, Max W. wrote:

def movie_file=(x)
#code
end

The above is used to SET the movie file

ahhh…of course :slight_smile: I always used attr_accessor in ruby (i’ve only done
a few bits of non-rails ruby coding) and forgot about the individual set
and get methods. That all makes sense now, thanks both!